Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undo a fast-forward merge

Tags:

git

I've made some changes to my git repo that I wish to undo.

My git repo looked like this:

A-B----     master    \   /     C-D     * develop 

I was on the develop branch, forgot that it differed from the master branch, made a change on develop, merged it into master, and then pushed to my remote (called publish).

Because there were no changes on master since B (common ancestor), git did a fast-forward merge.

Now, my repo looks like this:

A-B-C-D   master, develop, remotes/publish/master, remotes/publish/develop. 

I wanted to revert the last merge, restoring master to B.

From what I read in How to undo last commit(s) in Git?, I used git reset sha-of-B to restore my master branch to revision B.

Questions:

  • How do I restore develop to revision D?
  • How do I then push these changes back to remote/publish?
like image 407
Fabian Tamp Avatar asked Jan 13 '13 21:01

Fabian Tamp


People also ask

How do I turn off fast forward merge?

Disables the default fast forwarding on merge commits. Use git config --add merge. ff false to disable fast-forward merging for all branches, even if it is possible.

Can you revert a merge?

You can undo a Git merge using the git reset –merge command. This command changes all files that are different between your current repository and a particular commit. There is no “git undo merge” command but the git reset command works well to undo a merge.

How do I revert changes after 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 you undo a merge process?

How do I cancel a git merge? Use git-reset or git merge --abort to cancel a merge that had conflicts. Please note that all the changes will be reset, and this operation cannot be reverted, so make sure to commit or git-stash all your changes before you start a merge.


2 Answers

If you reset the branch master, it won't touch develop branch. In order to reput all in order, you should do:

git checkout master git reset --hard sha-of-B git checkout develop git reset --hard sha-of-D git checkout master git merge develop --no-ff 
like image 64
Simon Boudrias Avatar answered Oct 13 '22 05:10

Simon Boudrias


  • imagine you are still on master where you merged and pushed
  • git reset --hard @{1}
    • this resets branch “master” to where it was one step back on your computer (i.e. at “B”)
  • for develop do nothing, because it should still be at “D”
  • git push publish develop
    • this pushes branch “develop” in the usual way
  • git push publish master --force-with-lease
    • pushes branch “master” backwards, with the permission gained via --force-with-lease in a safe way
    • the use of --force might silently overwrite work from others, so I never use that
  • or both push operations together with this surprising syntax:
    • git push publish develop --force-with-lease=master
like image 40
Robert Siemer Avatar answered Oct 13 '22 04:10

Robert Siemer