how do you push only some of your local git commits?

前端 未结 5 1494
予麋鹿
予麋鹿 2020-11-29 15:02

Suppose I have 5 local commits. I want to push only 2 of them to a centralized repo (using an SVN-style workflow). How do I do this?

This did not work:

<         


        
5条回答
  •  [愿得一人]
    2020-11-29 15:30

    By default, git-push pushes all branches. When you do this:

     git checkout HEAD~3  #set head to three commits ago
     git push #attempt push from that head
    

    You move to a detached HEAD (you're not on any branch) and then you push all the branches, including the local master (which is still where it was) to the remote master.

    The manual solution is:

     git push origin HEAD:master
    

    If you find the default behaviour of pushing all branches confusing (and dangerous!), add this to your ~/.gitconfig:

     [remote.origin]
        push = HEAD
    

    Then only the branch you're on is pushed. In your example (a detached head), you would have got this error message, rather than accidentally pushing the wrong commits:

     error: unable to push to unqualified destination: HEAD
    

提交回复
热议问题