Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undo git fast forward merge

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

like image 795
Ivan Longin Avatar asked Jun 11 '13 09:06

Ivan Longin


People also ask

How do I turn off fast forward merge in git?

Use git config --add merge. ff false to disable fast-forward merging for all branches, even if it is possible. You can use the --global flag to configure this option globally.

How do I revert a git merge?

You can use the Git reset command to undo a merge. Firstly, you need to check for the commit hash (or id) so you can use it to go back to the previous commit. To check for the hash, run git log or git reflog . git reflog is a better option because things are more readable with it.

How do I undo a merge conflict?

In case you've made a mistake while resolving a conflict and realize this only after completing the merge, you can still easily undo it: just roll back to the commit before the merge happened with "git reset --hard " and start over again.


2 Answers

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.

like image 96
herman Avatar answered Sep 22 '22 07:09

herman


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>
like image 43
cforbish Avatar answered Sep 21 '22 07:09

cforbish