Document toolboxDocument toolbox

How to revert Git repository to a specific commit

In case you find an unexpected issue in a code you have already deployed or there is some mixup in the Git repository, you might want to revert to a previous state (commit) that you are sure is gonna work for you.

Step-by-step guide

For this to work you first need to find the hash string of the commit you want to revert to. The easiest way of achieving that is to look at the graphical view of the Git commit tree and find the needed commit.

In the example we have found an issue in the very latest commit to master branch:

First you can make sure on which commit you are with this command:

MacBook:git user$ git rev-parse HEAD
c8ecbc543b127958ce3a60646353893343ee6c58

As you can see we are on the very last commit. Now we need to find the full hash of the previous commit. To to that you can either use the command:

git log

or just double click the needed commit in SourceTree and copy it from there:

Once you have the needed commit hash you can check it out in your repository like this and make sure you have switched to it:

MacBook:git user$ git checkout 6107fedf6355e8b2d6df5353393bb7d5845a75d6
Note: checking out '6107fedf6355e8b2d6df5353393bb7d5845a75d6'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 6107fed... Merge pull request #9 in repository-name from feature/item-drilldown-on-module-production-reports-produceability to master
MacBook:git user$ git rev-parse HEAD
6107fedf6355e8b2d6df5353393bb7d5845a75d6

 

Now you are done and likely have to restart the server or in some cases even refresh HAL rules or other maintenance operations.