Search code examples
gitgit-mergegit-resetgit-revert

Is there a way to cleanly and easily undo a git merge?


I've read that doing git reset commitBeforeDoingMerge --hard would no undo the merge:

1pm-3pm This is my branch and I need to merge master into it
1pm-2pm This was master
1pm-2pm-3pm-4pm Merging ended up doing this, 4pm being the merge commit
1pm-2pm-3pm if This is the result if you do git reset 3pm --hard, because it does not undo the merge, it just goes back to that commit in the branch that was merged.

Now, another proposal is to do git revert 4pm -m 1, but that has a problem according to the manual:

Usually you cannot revert a merge because you do not know which side of the merge should be considered the mainline. This option specifies the parent number (starting from 1) of the mainline and allows revert to reverse the change relative to the specified parent.

Reverting a merge commit declares that you will never want the tree changes brought in by the merge. As a result, later merges will only bring in tree changes introduced by commits that are not ancestors of the previously reverted merge. This may or may not be what you want.

So, is there a way to cleanly and easily undo a git merge?


Solution

  • I'm still not sure what you are doing, but this is not how git merge works. I will try to recreate what you did and maybe you can spot the difference.

    First i created your two starting branches:

    git log --oneline --graph --all
    * 0593ba1 (HEAD -> test) 3pm
    | * eba415c (master) 2pm
    |/
    * 7f9a804 1pm
    

    Merging master into test. git merge master -m "4pm"

    git log --oneline --graph --all
    *   6be609f (HEAD -> test) 4pm
    |\
    | * eba415c (master) 2pm
    * | 0593ba1 3pm
    |/
    * 7f9a804 1pm
    

    now we reset test back to 3pm

    git reset --hard 0593ba1
    HEAD is now at 0593ba1 3pm
    
    git log --oneline --graph --all
    * 0593ba1 (HEAD -> test) 3pm
    | * eba415c (master) 2pm
    |/
    * 7f9a804 1pm
    

    now we see the 2pm commit is only in master, to confirm the log of only test

    git log --oneline
    0593ba1 (HEAD -> test) 3pm
    7f9a804 1pm
    

    that is how I merge and undo the merge, please feel free to let me know what you are doing differently or where I have not recreated you example how you need it to be.