Undo a Git merge that hasn't been pushed yet

前端 未结 30 2250
情歌与酒
情歌与酒 2020-11-21 22:27

Within my master branch, I did a git merge some-other-branch locally, but never pushed the changes to origin master. I didn\'t mean to merge, so I\'d like to un

30条回答
  •  梦谈多话
    2020-11-21 23:21

    1. First, make sure that you've committed everything.

    2. Then reset your repository to the previous working state:

      $ git reset f836e4c1fa51524658b9f026eb5efa24afaf3a36
      

      or using --hard (this will remove all local, not committed changes!):

      $ git reset f836e4c1fa51524658b9f026eb5efa24afaf3a36 --hard
      

      Use the hash which was there before your wrongly merged commit.

    3. Check which commits you'd like to re-commit on the top of the previous correct version by:

      $ git log 4c3e23f529b581c3cbe95350e84e66e3cb05704f
      
      commit 4c3e23f529b581c3cbe95350e84e66e3cb05704f
      
      ...
      
      commit 16b373a96b0a353f7454b141f7aa6f548c979d0a
      
      ...
      
    4. Apply your right commits on the top of the right version of your repository by:

      • By using cherry-pick (the changes introduced by some existing commits)

            git cherry-pick ec59ab844cf504e462f011c8cc7e5667ebb2e9c7
        
      • Or by cherry-picking the range of commits by:

        • First checking the right changes before merging them:

          git diff 5216b24822ea1c48069f648449997879bb49c070..4c3e23f529b581c3cbe95350e84e66e3cb05704f
          
        • First checking the right changes before merging them:

          git cherry-pick 5216b24822ea1c48069f648449997879bb49c070..4c3e23f529b581c3cbe95350e84e66e3cb05704f
          

          where this is the range of the correct commits which you've committed (excluding wrongly committed merge).

提交回复
热议问题