I had a “git” of a problem this week. (Ha… see what I did there?) I committed a bunch of changes to a remote branch, merged them to master, and then needed to back out those changes. (Why this need occurred is another story for another time.)
First, my git log
This is basically what my git log looked like.
Obviously, the plan was to revert back to fe4366a, a.k.a. “Commit I want to revert back to”.
What do I do now?
My first instinct was to use “git reset –hard fe4366a”. However, the problem with this command is that it only reset my working directory back to that “good” commit. No changes to the branch were made, so I have nothing to push back up to the remote server.
Git checkout to the rescue! Seriously. Here’s what I did:
Step 1: Create a new branch (based off of master) for my reversion.
Step 2: Checkout my last good commit to the new branch.
Step 3: At this point, all of the files that were modified in the 3 bad commits have been modified again locally, to match the version in the “Commit I want to revert back to”. Sanity check this just to be sure.
Step 4: After confirming that the reverted changes look OK, commit the changes to the branch.
Done! At this point, my-new-branch can be merged back up to master or you can do whatever you want with it.
Give credit where credit is due.
I found the solution on Stack Overflow, because that website is awesome. The specific answer is at http://stackoverflow.com/questions/4114095/revert-to-a-previous-git-commit
There are a few other suggestions on how to revert commits in Git in the answers on that page. The one I have outlined above was the one that worked best for my situation. YMMV. Good luck!