How to Check If a Git Commit Is Reverted

GitGitBeginner
Practice Now

Introduction

In this lab, you will learn how to identify if a Git commit has been reverted. We will explore how to use the git log command to find revert commits and then utilize git show to verify the details of these reverts. Finally, we will examine non-reverted commits to understand the difference in their history and content. This hands-on exercise will guide you through creating a sample project history, including a commit that is subsequently reverted, allowing you to practice the techniques for identifying and verifying reverted commits.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("Git")) -.-> git/BasicOperationsGroup(["Basic Operations"]) git(("Git")) -.-> git/BranchManagementGroup(["Branch Management"]) git/BasicOperationsGroup -.-> git/add("Stage Files") git/BasicOperationsGroup -.-> git/commit("Create Commit") git/BasicOperationsGroup -.-> git/diff("Compare Changes") git/BranchManagementGroup -.-> git/log("Show Commits") subgraph Lab Skills git/add -.-> lab-560064{{"How to Check If a Git Commit Is Reverted"}} git/commit -.-> lab-560064{{"How to Check If a Git Commit Is Reverted"}} git/diff -.-> lab-560064{{"How to Check If a Git Commit Is Reverted"}} git/log -.-> lab-560064{{"How to Check If a Git Commit Is Reverted"}} end

Run git log to Find Revert Commits

In this step, we will learn how to use git log to find commits that have been reverted. Reverting a commit means creating a new commit that undoes the changes introduced by a previous commit. This is useful when you've made a mistake and want to easily undo it without losing the history of the original commit.

First, let's make sure we are in our project directory. Open your terminal and navigate to the my-time-machine directory:

cd ~/project/my-time-machine

Now, let's create a few commits to simulate a project history, including a commit that we will later revert.

Create the first file:

echo "Initial content" > file1.txt
git add file1.txt
git commit -m "Add file1"

You should see output similar to this:

[master (root-commit) a1b2c3d] Add file1
 1 file changed, 1 insertion(+)
 create mode 100644 file1.txt

Now, let's add some more content and make another commit:

echo "Adding more content" >> file1.txt
git add file1.txt
git commit -m "Add more content to file1"

You should see output similar to this:

[master 4e5f6g7] Add more content to file1
 1 file changed, 1 insertion(+)

Next, let's make a commit that we will revert later:

echo "This commit will be reverted" > file2.txt
git add file2.txt
git commit -m "Add file2 (will be reverted)"

You should see output similar to this:

[master 8h9i0j1] Add file2 (will be reverted)
 1 file changed, 1 insertion(+)
 create mode 100644 file2.txt

Now, let's revert the last commit. We can use git revert HEAD to revert the most recent commit:

git revert HEAD --no-edit

The --no-edit flag tells Git to automatically create the revert commit message without opening an editor. You should see output similar to this:

[master k2l3m4n] Revert "Add file2 (will be reverted)"
 1 file changed, 1 deletion(-)
 delete mode 100644 file2.txt

Great! We have now created a commit that reverts the changes from the "Add file2 (will be reverted)" commit.

Now, let's use git log to view our commit history and find the revert commit:

git log --oneline

You should see output similar to this:

k2l3m4n (HEAD -> master) Revert "Add file2 (will be reverted)"
8h9i0j1 Add file2 (will be reverted)
4e5f6g7 Add more content to file1
a1b2c3d Add file1

Notice the commit message "Revert 'Add file2 (will be reverted)'". This clearly indicates that this commit is a revert of a previous commit. Using git log with the --oneline flag is a quick way to see a summary of your commit history and identify revert commits by their message.

Use git show to Verify Revert

In this step, we will use the git show command to examine the details of the revert commit and verify that it correctly undid the changes from the original commit. The git show command is a powerful tool for inspecting Git objects, including commits, tags, and blobs.

First, make sure you are in the ~/project/my-time-machine directory:

cd ~/project/my-time-machine

Now, we need the commit hash of the revert commit. You can get this from the output of git log --oneline from the previous step. It's the first commit listed. For example, if your git log --oneline output was:

k2l3m4n (HEAD -> master) Revert "Add file2 (will be reverted)"
8h9i0j1 Add file2 (will be reverted)
4e5f6g7 Add more content to file1
a1b2c3d Add file1

The commit hash for the revert commit is k2l3m4n. Note: Your commit hash will be different.

Now, use git show followed by the commit hash of your revert commit. Replace YOUR_REVERT_COMMIT_HASH with the actual hash from your git log --oneline output:

git show YOUR_REVERT_COMMIT_HASH

For example, using the example hash k2l3m4n:

git show k2l3m4n

You should see output similar to this:

commit k2l3m4n (HEAD -> master)
Author: Jane Doe <[email protected]>
Date:   Mon Aug 7 10:05:00 2023 +0000

    Revert "Add file2 (will be reverted)"

    This reverts commit 8h9i0j1.

diff --git a/file2.txt b/file2.txt
deleted file mode 100644
index 8b1a99c..0000000
--- a/file2.txt
+++ /dev/null
@@ -1 +0,0 @@
-This commit will be reverted

Let's break down this output:

  • The first part shows the commit details: the commit hash, author, date, and the commit message. Notice the message explicitly states that it reverts a specific commit (identified by its hash).
  • The diff section shows the changes introduced by this commit. In this case, it shows that file2.txt was deleted (deleted file mode 100644). This confirms that the revert commit successfully undid the creation of file2.txt.

Using git show allows you to inspect the exact changes introduced by any commit in your history. This is incredibly useful for understanding the history of your project and verifying that reverts or other changes were applied correctly.

Test Non-Reverted Commits

In the previous steps, we created several commits and then reverted one of them. We used git log to see the history and git show to inspect the revert commit. Now, let's verify that the commits that were not reverted still exist and contain the expected changes.

First, ensure you are in the ~/project/my-time-machine directory:

cd ~/project/my-time-machine

Recall our commit history from git log --oneline:

k2l3m4n (HEAD -> master) Revert "Add file2 (will be reverted)"
8h9i0j1 Add file2 (will be reverted)
4e5f6g7 Add more content to file1
a1b2c3d Add file1

The commits "Add file1" and "Add more content to file1" were not reverted. Let's use git show to inspect the "Add more content to file1" commit and see its changes.

Find the commit hash for "Add more content to file1" from your git log --oneline output. In the example above, it's 4e5f6g7. Remember to use your actual commit hash.

Now, use git show with that commit hash:

git show YOUR_MORE_CONTENT_COMMIT_HASH

For example, using the example hash 4e5f6g7:

git show 4e5f6g7

You should see output similar to this:

commit 4e5f6g7
Author: Jane Doe <[email protected]>
Date:   Mon Aug 7 10:02:00 2023 +0000

    Add more content to file1

diff --git a/file1.txt b/file1.txt
index 8b1a99c..a1b2c3d 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1 +1,2 @@
 Initial content
+Adding more content

This output shows the details of the commit and the diff section clearly indicates that a line "Adding more content" was added to file1.txt. This confirms that this commit and its changes are still part of the project history, even though a later commit was reverted.

This demonstrates a key aspect of Git: reverting a commit creates a new commit that undoes the changes. It does not erase the original commit from history. This is important because it maintains a complete record of all changes, allowing you to understand the full evolution of your project.

You can similarly use git show with the hash of the "Add file1" commit (a1b2c3d in the example) to see the initial creation of file1.txt.

By using git log to see the history and git show to inspect individual commits, you can effectively navigate and understand the timeline of your project, even when commits have been reverted.

Summary

In this lab, we learned how to identify reverted commits in a Git repository. We started by creating a project history with several commits, including one specifically intended for reversion. We then used the git revert HEAD --no-edit command to create a new commit that undoes the changes of the previous commit. This process demonstrated how reverting a commit adds a new commit to the history rather than removing the original.

We will continue to explore how to use git log to find these revert commits and git show to verify their contents, distinguishing them from non-reverted commits.