Git Revert: How to Undo Last Commit

March 19, 2020


Git is a popular project tracking application. As a project progresses through different revisions, those revisions are published as a commit. A commit represents a checkpoint, where a copy of the project is saved at a specific point.

This guide shows you how to undo the last commit in git.

How can I undo the last commit in git?


  • An existing project in Git
  • Access to a terminal window/command line

How to View Last Commit

Git offers many features to manage your project from different historical commits. For example, you can view an old commit, then create a branch from it.

A hash is an alphanumeric code that identifies each commit. Enter the following command to display a commit hash:

git log

The hash is necessary to display or manage a specific commit.

Example of Git commit hash

Examine Previous Commit

To analyze the status of your project from a previous commit, use the checkout command:

git checkout [hash]

When using a hash with a Git command, there is no need to type it in its entirety. The first few unique characters are enough for Git to identify an entry accurately.

Note: Find out how to Git checkout tags and why they are useful for your development project.

Revert Unpublished Commits

An unpublished commit is an update committed in Git but that has not been uploaded to a server. To reset to a previous commit, before any changes were made:

git reset --hard [hash]

This command wipes the slate clean back to the previous commit. Any changes you made will be lost after using the  reset --hard command.

If you want to preserve your work, you can use the stash command:

git stash
git reset --hard [hash]
git stash pop

The stash command saves the work you did, and stash pop retrieves those changes after the reset. Alternately you can use the following:

git reset --soft [hash]

This command resets the commit history, but it leaves your working directory and staging index as-is.

Revert Published Commits

Once a commit is uploaded to the server, it creates a more permanent project log. It is not advisable to use reset in this case. Other developers may have retrieved the updated project already.

Deleting updates from one system may cause conflicts with other team members. Instead, use the revert command:

git revert [hash]

Make sure to enter the code for the hash you want to revert to. The system asks you to enter a specific commit message for the changes the revert command is going to perform.

Define commit message for the revert action

This action creates a new commit based on the one you specified, with a revert tag. This acts as a log, showing that the commit was published and then reverted (instead of pretending it never happened).

Verify the status by entering:

git status

The current commit is running in detached head status

Status of reverted commit confirms that HEAD is detached.

To fix it, display the hash of the new (reverted) commit:

git log

You now see the hash of the new commit and the reverted commit as well.

System display hash of reverted commit and new commit

To view the latest (reverted) hash:

git checkout [hash]


You now know how to use the revert and reset commands to undo changes and revert to a previous commit. Additionally, you have learned how to check the status of your current commit.

Remember not to use reset for published commits as it may lead to version conflicts.

Was this article helpful?
Vladimir Kaplarevic
Vladimir is a resident Tech Writer at phoenixNAP. He has more than 7 years of experience in implementing e-commerce and online payment solutions with various global IT services providers. His articles aim to instill a passion for innovative technologies in others by providing practical advice and using an engaging writing style.
Next you should read
How to Undo and Redo Changes in Vim / Vi
April 28, 2020

Mastering basic Vim commands includes learning how to undo and redo changes in this text editor. Knowing how...
Read more
How To Switch Branch on Git
February 3, 2020

Developers need to switch between branches frequently. Git branches allow you to work on your code, fix bugs...
Read more
How to Install Git on CentOS 8
January 14, 2020

This article is a step-by-step tutorial on how to install Git on CentOS 8. As a popular version control...
Read more
How To Rename a Local and Remote Git Branch
January 6, 2020

Git is a version control system that helps you control the stages of software development. It uses named...
Read more
  • © 2021 Copyright phoenixNAP | Global IT Services. All Rights Reserved.