After doing it the wrong way (see ‘wrong ways to revert a commit’), here is a good way to revert a commit that accidentally inserted in the past.
Reverting a commit
- Checkout the wanted branch, with the bad commit (lets mark it as SHA-1).
- git revert –no-commit SHA-1
- Solve merge problem
- Commit at the end
Solving merge conflicts on the way
Assuming your mergetool is configured.
Note: see ‘config BC3 as a mergetool‘ if you need to config your mergetool to BC3.
Solve your merge using the following command:
git mergetool
Config BC3 as a mergetool
Follow the instructions here:
http://www.scootersoftware.com/support.php?zz=kb_vcs#gitwindows
Note: If you use the Git for Windows’ Bash Command Prompt instead of the default Windows Command Prompt, you need to escape the $ character with .
Wrong ways to revert a commit
I think that its important to know the wrong ways of doing things also, here are some I tried and my personal conclusions:
Comparing folders and files
After trying that option here are some of this strategy disadvantages:
- Need to compare a lot of files ==> lot of effort.
- Do a 2 way comparison and can’t know what was the base change to decide. so, you guess by memory.
- This solution is limited by your memory power, the bigger the change you made the bigger the effort.
- Its not safe, you can make human errors and effect others commits.
Cherry-pick
Another solution proposed, sounds good but have its limits:
If the commit to undo is far in the past, you’ll need to:
- go to one commit before the bad commit
- start cherry-pick commits one after the bad one.
- merge
While this is a solution, what you doing is – undoing one commit – the hard way, and this post describes exactly how to do this strategy in one strike.
Resources
Git – git-revert Documentation.