Git reverts made easy!

November 12, 2016

There are different ways to do git reverts, and it usually comes down to the developer; but check out the way I do it, especially if you’re new to using Git or source control.

There are multiple ways of performing git reverts, but most times it can be a headache when it’s not just you on that project.  The following way is how I do it because of the following reasons:

  • It’s easier and straightforward.
  • It allows other devs to code review the revert.
  • It’s also easier to revert the revert.

I’ve seen some developers simply revert the commits on the master or develop branches which puts the project at risk of introducing bugs and headaches all around.  I’ve seen others create a revert branch and revert the individual commits related to those changes one by one which isn’t all too efficient, but it’s a bit safer to merge in the reverts into the develop branch afterwards.  The way I do it is by creating a revert branch and then reverting the MERGES, not the individual commits, before merging the revert branch into the develop branch.

The directions are fairly simple:

1. Create a new branch locally to be your revert branch.  For example:

$ git checkout -b projectRevertBranch

2. Look for the commit hash that is a MERGE.  You can either do this by looking it up using git log OR if you use Github or Bitbucket, then you should be able to grab the commit hash from there too.  I usually just grab the first 7 characters of the hash to use.

3. When you have the commit hash, you can start your revert like so:

$ git revert -m 1 0ff18c

Note: you NEED “-m” in order to revert merges.  The 1 afterwards is to tell git that you want the parent number of 1, which is usually your feature branch that you wanted to revert in the first place.

4. When you have reverted all the merges that need to be reverted, then push your revert branch up to the remote repository like Github or Bitbucket.  

5. Then create a pull request to get the reverts merged into the develop or whatever branch needed it.  Done!

If you need the same changes to come back, you can either revert the reverts or just push your feature branch back up to the remote repo and create a pull request.  Simple and easier to follow right?  Let me know if you have any thoughts or suggestions, or maybe you might have an easier process for performing reverts?

You Might Also Like