Skip to main content
  1. Learn
  2. Software Development
  3. Guides
  4. Git tutorial
  5. How to use Git
  6. How to manage history in Git
  7. Revert a commit
GuidesSoftware DevelopmentBacklog
Git

Project and code management together.

Revert a commit

Reverting a commit in Git is a valuable skill that allows you to correct mistakes, undo changes, or manage your commit history effectively. Whether you need to remove the last commit, discard specific changes, or entirely erase a commit, Git provides various tools to help you undo commits and maintain a clean development history.

In this part of our tutorial, we will explore the process of reverting a commit in Git.

Go to the git-tutorial/tutorial2 directory you previously downloaded.

When you examine the history of this repository, it will look like the following:

Current history

We will undo the “append description of the pull command” commit using the git revert command.

First, open the sample.txt file and verify that its content looks like the following:

Anyone can learn Git with this tutorial and Backlog
add: Register a change in an index
commit: Save the status of an index
pull: Obtain the content of the remote repository

We will undo the latest HEAD commit titled “append description of the pull command” using the revert command, as follows:

$ git revert HEAD
[main d47bb1d] Revert "append description of the pull command"
  1 files changed, 1 insertions(+), 2 deletions(-)

Now when we open sample.txt you will see that the last line (i.e., “pull: Obtain the content of the remote repository”) will no longer exist.

The history of repository after the revert

Use the git log command to verify that the new history looks like the following:

$ git log
  commit 7bcf5e3b6fc47e875ec226ce2b13a53df73cf626
  Author: yourname <yourname@yourmail.com>
  Date:   Wed Jul 18 15:46:28 2022 +0900

      Revert "append description of the pull command"

      This reverts commit 0d4a808c26908cd5fe4b6294a00150342d1a58be.

  commit 0d4a808c26908cd5fe4b6294a00150342d1a58be
  Author: yourname <yourname@yourmail.com>
  Date:   Mon Jul 16 23:19:26 2022 +0900

      append description of the pull command

  commit 9a54fd4dd22dbe22dd966581bc78e83f16cee1d7
  Author: yourname <yourname@yourmail.com>
  Date:   Mon Jul 16 23:19:01 2022 +0900

      append description of the commit command

  commit 326fc9f70d022afdd31b0072dbbae003783d77ed
  Author: yourname <yourname@yourmail.com>
  Date:   Mon Jul 16 23:17:56 2022 +0900

      append description of the add command

  commit 48eec1ddf73a7fb508ef664efd6b3d873631742f
  Author: yourname <yourname@yourmail.com>
  Date:   Mon Jul 16 23:16:14 2022 +0900

      first commit

Subscribe to our newsletter

Learn with Nulab to bring your best ideas to life