Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing/undoing a merge on Sourcetree

I made quite few changes in my project (I was working on a remote branch and not the master), I committed them and created a pull request on BitBucket and merged the branch to master. I had forgotten to push my changes after the commit. Now, after trying switching the current branch to my remote branch and reverting to the commit before the merge, I managed to get all my changes back and back them up elsewhere in my system. What I want to do now is undo the bad merge that I did. Each time I click on the merge and select "Reverse commit", I get the following error message:

"error: Commit is a merge but no -m option was given.

fatal: revert failed"

The branches look like this now:

enter image description here

I want to remove the merge and bring it to a state such that it doesn't say master(4 behind) anymore.

like image 491
Rameez Hussain Avatar asked Mar 20 '14 15:03

Rameez Hussain


People also ask

How do I undo a merge?

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!

How do you undo a merge that has been pushed?

Now, if you have already pushed the merged changes you want to undo to your remote repository, you can right-click on the merge commit and select Revert commit from the context menu. You will then be asked if you want to immediately create a commit.


1 Answers

This method is based on history deletion:

  1. Check out the branch you made the mistake on
  2. Right click on the commit you want to reset the branch to
  3. Click "Reset current branch to this commit"
  4. Select "Hard" mode and click "OK"
  5. Unfortunately you need terminal to do this bit. Type git push origin name_of_branch --force into terminal (you may need to enter your git repo username and password for it to accept the command)

Update: The current version of source tree supports forced pushes. That means you don't need to use the command mentioned in step 5! There is a check box visible when pushing that is labled "force". It is usually disabled.

How to enabled the force push checkbox:

  1. "Tools" (in the blue bar at the top of the screen)
  2. "Options"
  3. "Git" tab
  4. "Enable Force Push" (2nd section)
  5. "OK"

Check that checkbox when pushing to also fix the mistake on the origin server.

This is probably the easiest way to do it but since it is based on history deletion, If others are working on the project, make sure to let them all know what you are doing so you don't break anyone's git repository.

Update 2: If you use Visual Studio Code as your code editor, I highly recommend installing the "Git Graph" extension. It allows you to do practically everything that Source Tree allows you to do, but you can do it from directly inside your code editor! The steps for doing this using Git Graph are roughly the same as the steps for doing this in Source Tree.

like image 158
Daniel Tonon Avatar answered Oct 08 '22 04:10

Daniel Tonon