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:
I want to remove the merge and bring it to a state such that it doesn't say master(4 behind)
anymore.
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!
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.
This method is based on history deletion:
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With