Sergey Nivens - Fotolia

Get started Bring yourself up to speed with our introductory content.

How to roll back Git code to a previous commit

Mistakes happen. Thankfully, in Git, admins have two command options to roll back to a previous commit. Learn more about both here.

With Git, IT teams can implement version control. Humans make mistakes, and consequently need to roll back to previous versions of content.

Let's look at how to roll back Git commits to a known-good version, as well as the implications and potential complications of doing so.

What is git reset?

Every time an IT admin commits a Git deployment, that latest commit becomes the head, or tip, of the code tree -- in other words, the current version. The Git commit process provides a point-in-time snapshot (PIT snapshot) of the version-controlled files at the time of every change.

An administrator can roll back the code repository to a previous commit -- that point-in-time copy -- in several ways, depending on the end goal. One approach is the git reset command.

Before using this command, you must understand what git reset does. Outcomes can vary between command uses, and with which switches. Use the command with caution. There are two modes for git reset:

  • Soft. This mode resets the code tree's head to the designated former commit instance. All the files between that PIT snapshot and now are set to staged -- ready to commit but not yet committed. This is the default mode.
  • Hard. Use this mode with extreme caution. These changes can't be reverted. This command will reset everything, move the head back to the indicated commit version and remove all changes added to the code tree after that specific version number. In effect, the git reset command instantiates a 'hard deletion' of all changes from now -- or point-in-time of code reversion -- to the designated former code commit. It resets the code tree to the version in question and deletes unstaged files.

How to undo a Git commit

First, decide how far back to go into the version history. To view the previous commits, use the git log –-oneline command. This provides the commit details.

Once the IT team chooses a code version to which their tree should revert, use the commit ID to execute the command. In the following example, x12345 represents the commit ID, gained from the git log output:

git reset x12345

Alternatively, there is a shorthand method to roll back the commit: To revert commits without knowing the necessary commit ID, admins can use the command below to revert code versions relative to where the current head is. In the example, ~1 refers to the number of commits backward to which the code tree will revert. Figure 1 illustrates the results for adding several commits and then reverting back one version.

git reset head~1

Fig. 1 reverting images in git
Figure 1

Admins can also use git revert. This command undoes the effects of a bad or incorrect commit. It creates a new head without the issues of the bad commit but doesn't revoke any previous work. However, this version contains history from the bad commit. This inclusion is by design: Git wants to track history accurately, and to delete current heads would create massive gaps in the system history.

With experimentation comes experience. But as noted above, take caution to avoid file loss.

Dig Deeper on Configuration Management and DevOps