git: push a single commit

后端 未结 4 758
一整个雨季
一整个雨季 2020-12-02 22:33

Say I made several commits and wish to cherry pick which ones I push to the remote repository. How can I do that (in ascii: C1->C2->C3->C4 and I want to push C2 and C4). Wil

相关标签:
4条回答
  • 2020-12-02 22:41

    IIRC, due to how git considers commits to work, C4 inherently includes C3, so the concept of "pushing C4 but not C3" doesn't make sense to git (and likewise C2 relative to C1). (See the answer to this previous question.)

    0 讨论(0)
  • 2020-12-02 22:48

    What you're looking for:

    git push origin commit-id:master
    

    Credit goes to: http://blog.dennisrobinson.name/push-only-one-commit-with-git/

    Explanatory notes:

    • Pushing a commit pushes all commits before it (as Amber said). The key is to reorder your commits (git rebase -i) first, so they are in the order you want to push them.
    • The suggested branch + cherry-pick method (suggested by midtiby) works too, but why create throwaway branches when you don't need to.
    • commit-id doesn't have to be a sha1. To push everything before the last N commits, use "HEAD~N" in place of commit-id.

    If pushing to a branch that doesn't exist in the remote repository yet, prefix the remote branch with refs/heads/, such as:

    git push origin HEAD~1:refs/heads/completely-new-branch
    

    (If not, git will punish you with this hopeless error message).

    0 讨论(0)
  • 2020-12-02 22:51

    If you have your commits on a private branch, you can cherry pick commits from the private branch and apply them to the official branch. At this point you can now push all your commits on the official branch (which is the subset that you previously cherry picked).

    0 讨论(0)
  • 2020-12-02 23:04
    $ git push <remote name> <commit hash>:<remote branch name>
    
    # Example:
    $ git push origin 2dc2b7e393e6b712ef103eaac81050b9693395a4:master
    
    0 讨论(0)
提交回复
热议问题