20

I have this situation : Working on some testBranch for some time and I wanted to sync with master so I did git checkout testBranch and git merge master/testBranch

So now my branch is synced with master but then I found out that I want to undo that merge and problem is that merge is done with fast forward and now my commit history is mixed with master commits and testBranch commits and I don't know how to revert to state before merge on my testBranch.

Thanks for any help

2
  • Are commits merged from master/testBranch located at the tip of your testBranch or you've already managed to record several your own local commits on top of what has been merged? The way to recover heavily depends on this.
    – kostix
    Commented Jun 11, 2013 at 10:12
  • Any reason you're not satisfied with my answer?
    – herman
    Commented Oct 28, 2019 at 14:55

2 Answers 2

41

git reflog show testBranch

should show the Fast-forward merge as last item ({0}). After making sure this is the case, checkout testBranch and then just do

git reset --keep testBranch@{1}

to revert to the previous state.

3
  • This is a good answer. I would add that it's important to make sure you checkout testBranch before running git reset
    – jbll
    Commented Feb 12, 2016 at 17:39
  • 2
    An easy-to-understand and nice approach! This should be marked as an answer Commented Jun 27, 2016 at 8:50
  • Note for PowerShell users: You must run last command as git reset --keep 'testBranch@{1}' Commented Mar 22, 2023 at 16:36
5

If you know of a revision in which you want your local testBranch, it is as simple as:

git checkout testBranch
git reset --hard <revision>

If you have changes mixed in like (oldest on top):

<point>
<your_change_a>
<change_from_someone_else>
<your_change_b>
<testBranch>

You could:

git checkout testBranch
git reset --hard <point>
git cherry-pick <your_change_a>
git cherry-pick <your_change_b>
2
  • 1
    This should work. I would also recommend using a Git graphical viewer such as gitg or gitk to get a good overview of the changes you want to undo.
    – RyPeck
    Commented Jun 11, 2013 at 14:04
  • 1
    Thanks for your answer, but I have found another solution (I haven't try yours but I suppose it works to). Solution is found in this blog : blog.tplus1.com/blog/2011/09/23/undo-a-fast-forward-git-merge ... hope it helps someone else to :) ... Commented Jun 13, 2013 at 10:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.