Removing/undoing a merge on Sourcetree

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.


It's very unfortunate that SourceTree doesn't make it easy for you to revert merge commits (at least in Windows SourceTree 1.5.2.0). However, reverting the merge can easily be accomplished from the command line, but do you want to revert the merge by adding another commit that is the reverse of the results of the merge commit, or do you want to just remove the merge commit from history altogether?

If you're not sharing your master branch with other people, the simplest thing to do would be to simply do a hard reset to remove the merge commit from history. However, if other people already have a copy of that merge commit, then you'll create extra work for them as they try to re-sync their copies of master with the rewritten version of yours.

So you need to figure out which option you want to use. I will give the steps for both, using the command line:

Remove Commit from History

git checkout master
git push origin master --force

That will overwrite the merge commit on origin/master with the current state of your local master branch, thus removing the merge commit.

Revert Commit with another Reverse Commit

git checkout master
git merge origin/master
git revert -m 1 HEAD

That will bring your local master branch in-sync with origin/master, and then you can add a reverse commit by telling git revert that the 1st parent is the one that should be considered the "mainline" parent, and it will create the reverse commit relative to the changes brought in from the other parent commit.

If later on you decide that you want to bring in the changes from the other parent again, then you'll need to add another reversion commit that reverts the reversion commit that you just made (a technique known as "reverting the revert").

See also:

  1. Undo a Git merge?.
  2. How do I “un-revert” a reverted Git commit?.
  3. git-revert(1) Manual Page.
  4. How to revert a faulty merge.

Follow this step by step:

REVERTING A MERGE COMMIT

Master was merged and pushed into Develop with conflicts/unwanted codes.

To revert using Sourcetree:

  1. Select Develop, Select the last commit to which branch has to be reverted.
  2. Right click on that branch and select “Reset develop to this commit”.
  3. From the pop up select hard from drop down at the left bottom. Its done.

There should be no pull thereafter. If any pull occurs revert using terminal as below.

Now Merge master into develop.

To revert using Terminal:

  1. Select Develop, Select the last commit to which branch has to be reverted.
  2. Open terminal from the Sourcetree
  3. type git reset --hard <commit_before_merge id long one>
  4. If any pull count appear in the sourcetree then type git push -f, and its done.

Merge master into develop.


I found another way without command lines, a bit ugly but does the job.

So, do a hard reset to the commit you want to roll back, go to the project folder (Using Finder on Mac or Explorer on Windows) and make a copy of the whole folder. What you have inside this 'Copy' folder is the point you want to be at the end of this process.

Well... go back to source tree and then checkout the Head (the latest commit into your remote), then navigate again to the project folder, be sure you can see hidden folders because you must be able to see a folder called ".git"

project folder

Delete everything BUT ".git" from your current project, it means your current project has nothing inside but the folder called ".git", then navigate to your 'Copy' folder and copy everything but ".git" folder and paste the content inside your current project (the one with ".git" folder only)

Done. Go to source tree and commit the changes, your project is exactly where you wanted and all changes removed.

The end.


Obs1: Delete the "Copy" folder now to clean your pc from dirty files.

Obs2: This process don't remove your changes from Git, the commits will be there, what you are doing is deleting your changes and committing it.