How to Revert a Git Repository to a Previous Commit

While working with Git, it is often required to get back to one of the older commits to view its state or to delete the commits coming after it. Below, we will discuss the mentioned scenarios.

Watch a course Git & GitHub - The Practical Guide

How to temporarily switch to a different commit

To go back to an older commit temporarily, you can use the git checkout command by mentioning the commit hash:

git checkout <sha1-commit-hash>

The command above will detach your HEAD, that is, leave you with no branch checked out. To make commits after detaching from your branch, you need to create a new branch on the basis of that commit by using the command below:

git switch -c <new-branch-name>

Instead of using the 2 commands above, you can run the command below to create a new branch on the basis of an older commit :

git checkout -b <new-branch-name> <sha1-commit-hash>

How to remove unpublished commits

If you want to delete the recent commits existing only on your local repository, run the command below:

git reset --hard <sha1-commit-hash>

The command above will delete all the recent commits up to the one you have mentioned the hash for. The mentioned commit will be the most recent one

In case you have uncommitted local changes on your branch, this command will delete all of them.

If there are changes that you want to keep, act as follows:

git stash
git reset --hard <sha1-commit-hash>
git stash pop

This saves the modifications, then re-applies that patch after resetting.

Merge conflicts may arise if you have modified things which were changed since the commit you reset to. If you mess it up, you can go back anytime. To learn how to do that, check out How to Undo Git Reset.

How to undo published commits

In this section, we will demonstrate two ways of undoing the published commits, depending on whether you want to save the history or not.

Remove the published commits

As it was demonstrated in the previous section, it is possible to remove local commits with the git reset --hard command. After that, you can run git push with --force option and remove the commits from the remote repository.

git push --force origin HEAD
--force overwrites the remote branch on the basis of your local branch. It destroys all the pushed changes made by other developers. It refers to the changes that you don't have in your local branch.
git push --force-with-lease origin HEAD
--force-with-lease is considered a safer option that will not overwrite the work done on the remote branch in case more commits were attached to it (for instance, by another developer). Moreover, it helps you to avoid overwriting another developer's.

Revert the published commits

If you have published your work and don't want to reset the branch, you can revert the commits. Use git revert so as not to rewrite any history:

git revert <sha1-commit-hash>

If you don't want to add a separate commit for revert, after reverting you can run git rebase in the interactive mode to combine the latest commit with the commit of the revert. To learn how to do that check out How to Combine Multiple Git Commits into One.

git rebase -i HEAD~2

The git revert Command

Git revert belongs to the “undo” category operations, yet it’s not a classic undo command. The git revert command is applied to undo changes to the commit history of the repository. The git revert command allows you to pick a single commit, converse the changes and, finally, generate a completely new one. In the scope of this tutorial, you will learn how to revert to the previous state by using Git commands.

Generally, we can distinguish 2 significant advantages of git revert. They are as follows:

  1. The revert command can be effectively applied to a specific commit.
  2. It doesn’t change the history of the project. This means that it’s a completely secure operation for commits, which were already sent to the shared repository.

The git revert and git reset Commands

The most significant difference between git revert and git reset is that the git revert command targets a specific commit not removing all the coming commits. On the contrary, using the git reset command will remove the overall coming commits. Let’s say you are going to undo a commit with the usage of the git reset command, you need to remove the whole commits coming after the targeted one. Generally, git revert is the most secure alternative of git reset. Although the process may seem quite complicated, it becomes pretty easy after going through it several times.