How to Revert Back to a Previous Commit in the Repository in Git

Azhar Bashir Khan Feb 02, 2024
  1. Using git reset to Reset Back to a Previous Commit in the Git Repository
  2. Using git revert to Revert Back to a Previous Commit in the Git Repository
How to Revert Back to a Previous Commit in the Repository in Git

In this tutorial, we will learn how to revert to a previous commit in the repository in Git.

Git, a version control system, is used in a collaborative development environment to keep track of the changes done to the files.

Git is used to capture the snapshots of the changes of the files in a project directory and associates them with commits.

In Git, using commits one can browse and view the history of changes done to files.

We can also use Git to reset or revert back the project directory’s files in the Git repository to a previous commit (i.e.) the state of the files in the directory, when the commit was created.

We will now illustrate this with an example.

Using git reset to Reset Back to a Previous Commit in the Git Repository

In a collaborative development environment, we use Git to keep track of the changes done to files in the project directory in a Git repository.

When we create a commit to save our work, Git creates a unique ID (a.k.a. the “SHA” or “hash”) that allows us to keep a record of the specific changes committed along with who made them and when.

A commit is an individual change to a file (or set of files). The commits usually contain a commit message which is a brief description of what changes were made.

Sometimes, we may require revert or reset back the repository of the project directory to a previous commit.

Suppose we have the following commits as shown by the git log command in our repository.

$ git log --oneline
e4cd6b4 (HEAD -> main, origin/main) my change
99541ed second change
41f1f2a first change
...

Now, let’s say we want to reset our repository back to the previous commit given by SHA 41f1f2a with the comment first change.

One way to do that is to temporarily switch to the previous commit by using the git checkout command.

Thus, we would do as follows.

$ git checkout 41f1f2a

We can also create a new branch with the previous commit, so we can commit the new changes on it in that branch.

Thus, we would do as follows.

$ git checkout -b first-change-branch 41f1f2a

Instead, if we want to discard the changes since the previous commit, we would use the git reset command.

The syntax of the git reset command to reset the repository back to a previous commit is git reset -hard <commit-sha-id>.

Thus, in our case, we would do as follows.

$ git reset --hard 41f1f2a

Please note to use this with caution; this will also discard any local modifications. Any uncommitted changes will be lost.

Alternatively, we can stash the changes before doing the reset, as follows.

$ git stash
$ git reset --hard 41f1f2a
$ git stash pop

After executing the above commands, the local modifications are saved in the stash; and then, after reset to the previous commit, those modifications are re-applied to the repository.

Using git revert to Revert Back to a Previous Commit in the Git Repository

The git revert command is used when we want to keep the history of the repository.

Upon executing the command git revert, Git creates a commit with the reverse patch to nullify the previous commit. This way, we don’t rewrite any history.

The syntax of git revert command to revert back the repository back to a previous commit is, git reset <commit-sha-id1> <commit-sha-id2> ....

Thus, we want to revert the first two commits to revert the repository to the commit given by SHA 41f1f2a.

$ git revert e4cd6b4 99541ed

It will revert the repository by the given two commits.

We can also execute the git revert command as follows.

$ git revert HEAD~2..HEAD

The above git revert command would revert the last two commits.

Finally, as mentioned before, the git revert command creates a commit to cancel out the previous commit. Thus, we would now need to save this commit.

We need to do as follows.

$ git commit -m "reverted commits e4cd6b4 99541ed"

Thus, now the reversion of the commits is now saved in the repository as a new commit.

In some cases, there is a merge commit present and we may want to revert it too.

We can use the git revert command with the option -m parent-number; this option specifies the parent number (starting from 1) of the mainline and allows revert to reverse the change relative to the specified parent.

The merge commit has multiple parents. The git revert command needs additional information to decide which parent of the merge shall be considered as the mainline.

The syntax of the command is, git revert -m 1 <commit-sha-id>, the 1 is used for the first parent as the mainline.

Thus, suppose the commit SHA e4cd6b4 is a merge commit. Then we can do as follows.

$ git revert -m 1 e4cd6b4

Thus, we have elaborated on how to revert to a previous commit in the repository in Git.

For more information, please visit below.

  1. git-reset
  2. git-revert

Related Article - Git Reset

Related Article - Git Revert