Git Revert Commit: How to Undo Last Commit

March 3, 2022

Introduction

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 revert the last commit in git?

Prerequisites

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

How to Revert Commit

Git offers many features to manage your project from different historical commits. You can view and revert a previous commit, either published or unpublished.

Read on to learn how to identify and revert a published commit. Also, if you want to learn more about Git, check out our article on how does Git work.

Identifying the Commit

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

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 use Git checkout tags and why they are useful for your development project.

Git Revert: Reverting the Last Git Commit

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 as 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).

Git Reset: 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 Git stash:

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.

Note: Git stash allows users to save their uncommitted files and work in progress to a local stash. Learn how to Git stash specific files.

Git Amend: Modify the Last Commit

To modify the most recent commit, use the git commit --amend command. This command replaces the last commit with the amended commit without altering the snapshot.

The --amend flag is usually used to fix minor mistakes.

For instance, you want to commit a couple of files in a single snapshot but forget to add one of the files before committing. This issue is easily solved using the --amend option.

First, you add the missing file using:

git add file2.py

Then, run the command:

git commit --amend --no-edit

The --no-edit option amends the commit without changing the commit message.

Note: If you need to restore a missing repository, refer to our guide How to Restore a Git Repository.

Conclusion

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?
YesNo
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
  • © 2022 Copyright phoenixNAP | Global IT Services. All Rights Reserved.