Signup/Sign In

How to Undo Last Git Commit

Commits are used when we want to permanently store a change in our repository. But there may be cases where we realize that we no longer need that commit. We can always undo the Git Commit and go back to the former state of our repository. Let's learn how to undo a commit.

Going back to a previous commit

Undoing Committed Changes

There are three ways in which we can undo changes in Git but it depends on the situation and how you want to undo the commits. Let's learn how to undo commits in Git.

Using Git Checkout

  • Git Checkout command is used to navigate to different branches and can also be used to create new branches. We can also check out a previous commit using this command. But checking out a previous commit takes our repository in a Detached HEAD state. This happens because our HEAD is no longer at the tip of the branch and is pointing directly to a commit.

Using Git Checkout to go back to a previous commit

  • After going back to a previous commit we can create a new branch based on that commit and start working on that branch. This newly created branch will no longer have unwanted commits. Consider the following example where we created a new branch and added two new commits to it. The commit point C is no longer a part of the new feature branch.

Creating a new branch and adding commits to it.

  • To go back to the previous commit, the Git Checkout command will need its commit hash. To undo the last commit we will need the hash of the second last commit.
$ git checkout <commit-hash>
  • After doing that we can use the -b flag to create a new branch based on this commit.
$ git checkout -b <new-branch-name>
  • However, this method cannot be used if we do not want to create a separate branch or the branch with the unwanted commit is the master branch.

Using Git Reset

  • We can undo a commit by using the Git Reset command. This command works in three modes - Soft, Mixed, and Hard. These three modes will take us back to a previous commit, but they differ in how they treat changes in the working directory and the staging area.
  • Soft won't affect the working directory or the staging area. The Mixed option will revert the changes in the staging area. The Hard option will revert changes in the working directory along with the staging area and the repository.
  • It all comes down to what one wants to undo. If it is just the commit then --soft is the preferred option. It will keep the changes in the files and the staging area but will remove the commit. But if the commit was a complete failure and you also want to undo the changes of the files involved in that commit then the --hard option is preferred.
$ git reset --soft HEAD~1
  • We can also use the Git Reset command to revert changes to some other commit. We can mention the commit hash of that commit or we can mention its position relative to our HEAD. For example, HEAD~N will reset our head to the Nth commit before our HEAD.
  • One thing to note is that we should not use the Git Reset command if we intend to push to a remote repository. This is because the remote repository may still have the commit which we have removed from our local repository. Git will not allow us to push to the remote repository because of this conflict.

Consider the following example in which we have used the --soft option. We can see that commit is removed from the history but the changes in the staging area and working directory are intact.

Status of our repository before resettingAfter resetting, head points to a previous commit but the staging area and working directory are as it is.

Using Git Revert

  • As discussed above the Git Reset command cannot be used when working with a remote repository. We use the Git Revert command to undo commits on a repository that will later be pushed to a remote repository.
  • Git Revert works in a slightly different manner. It reverts all the changes made by the previous commit and then creates a new commit recording this reversal of the changes. So the unwanted changes are removed from the files but the commit is still present in our branch history.
  • After running the Git Revert command Git will open the text editor for us to change the commit message of the new reverted commit.
$ git revert HEAD

Consider the following example where we want to undo the last commit.

Viewing the previous commits using Git Log

Using the Git Revert command to undo the previous commit

When we run the Git Revert command a new commit is created after the unwanted commit which has all the reverted changes.

Viewing the new commit that was added by Git Revert

Summary

We will often commit our changes to the repository but sometimes we may need to undo these committed changes. Git provides us with several ways of doing that. We have the Git Checkout command, the Git Reset command, and the Git Revert command for undoing changes. The Git Reset is the most preferred command for this purpose as it cleanly removes the unwanted commits. Git Reset keeps the rest of the history of our project intact and it appears as though the unwanted commit never existed. But when pushing changes to a remote repository the Git Revert command is the preferred option.