Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rollback a Git merge

Tags:

git

develop branch
--> dashboard (working branch)

I use git merge --no-ff develop to merge any upstream changes into dashboard

git log:

commit 88113a64a21bf8a51409ee2a1321442fd08db705
Merge: 981bc20 888a557
Author: XXXX <>
Date:   Mon Jul 30 08:16:46 2012 -0500

    Merge branch 'develop' into dashboard

commit 888a5572428a372f15a52106b8d74ff910493f01
Author: root <[email protected]>
Date:   Sun Jul 29 10:49:21 2012 -0500

    fixed end date edit display to have leading 0

commit 167ad941726c876349bfa445873bdcd475eb8cd8
Author: XXXX <>
Date:   Sun Jul 29 09:13:24 2012 -0500

The merge had about 50+ commits in it, and I am wondering how to just revert the merge so dashboard goes back to the state pre-merge

The second part of this is, if I dont do merge with --no-ff, I don't get the commit 'Merge branch 'develop' into dashboard' .. How would I roll that merge back?

like image 504
cgmckeever Avatar asked Jul 30 '12 13:07

cgmckeever


People also ask

How do I revert a merge in git?

In case you are using the Tower Git client, undoing a merge is really simple: just press CMD+Z afterwards and Tower will undo the merge for you!

Can you revert a merge commit?

Just make sure you understand that if you revert the merge commit, you can't just merge the branch again later and expect the same changes to come back. But you can revert the revert to get them back if really needed. Thanks.

How do I undo a merge attempt?

Git merge --abort # this will allow you to undo merge conflicts. This attempts to reset your working copy to whatever state it was in before the merge. That means that it should restore any uncommitted changes from before the merge, Generally, you shouldn't merge with uncommitted changes anyway.


5 Answers

Reverting a merge commit has been exhaustively covered in other questions. When you do a fast-forward merge, the second one you describe, you can use git reset to get back to the previous state:

git reset --hard <commit_before_merge>

You can find the <commit_before_merge> with git reflog, git log, or, if you're feeling the moxy (and haven't done anything else): git reset --hard HEAD@{1}

like image 117
Christopher Avatar answered Oct 02 '22 03:10

Christopher


From here:

http://www.christianengvall.se/undo-pushed-merge-git/

git revert -m 1 <merge commit hash>

Git revert adds a new commit that rolls back the specified commit.

Using -m 1 tells git that this is a merge and we want to roll back to the parent commit on the master branch. You would use -m 2 to specify the develop branch.

like image 27
sturrockad Avatar answered Oct 02 '22 05:10

sturrockad


Just reset the merge commit with git reset --hard HEAD^.

If you use --no-ff git always creates a merge, even if you did not commit anything in between. Without --no-ff git will just do a fast forward, meaning your branches HEAD will be set to HEAD of the merged branch. To resolve this find the commit-id you want to revert to and git reset --hard $COMMITID.

like image 31
Nico Erfurth Avatar answered Oct 02 '22 05:10

Nico Erfurth


git revert -m 1 88113a64a21bf8a51409ee2a1321442fd08db705

But may have unexpected side-effects. See --mainline parent-number option in git-scm.com/docs/git-revert

Perhaps a brute but effective way would be to check out the left parent of that commit, make a copy of all the files, checkout HEAD again, and replace all the contents with the old files. Then git will tell you what is being rolled back and you create your own revert commit :) !

like image 23
Jorge Orpinel Pérez Avatar answered Oct 02 '22 03:10

Jorge Orpinel Pérez


If you merged the branch, then reverted the merge using a pull request and merged that pull request to revert.

The easiest way I felt was to:

  1. Take out a new branch from develop/master (where you merged)
  2. Revert the "revert" using git revert -m 1 xxxxxx (if the revert was merged using a branch) or using git revert xxxxxx if it was a simple revert
  3. The new branch should now have the changes you want to merge again.
  4. Make changes or merge this branch to develop/master
like image 24
Rohin Tak Avatar answered Oct 02 '22 04:10

Rohin Tak