How to View Changes in a Specific Git Commit

GitBeginner
Practice Now

Introduction

Git is a powerful version control system that helps developers track changes, collaborate, and manage their codebase effectively. A fundamental skill in Git is the ability to inspect the project's history to see what changes were made, when, and by whom. In this lab, you will learn how to view the changes introduced in a specific Git commit. This is essential for understanding the evolution of your project, reviewing code, and troubleshooting issues.

Exploring the Commit History

Before you can view the changes in a specific commit, you first need to find that commit. The git log command is used to display the commit history of a repository. For this lab, a sample Git repository has been created for you at ~/project/git-demo.

First, navigate to the project directory. All commands in this lab should be run from within this directory.

cd ~/project/git-demo

Now, use the git log command with the --oneline option to see a compact view of the commit history. This will show you a list of all commits, each with its unique commit hash and commit message.

git log --oneline

You should see an output similar to this. The commit hashes on your screen will be different, but the messages will be the same.

a1b2c3d (HEAD -> master) Add application file
e4f5g6h Update README with project description
i7j8k9l Initial commit: Add README.md

Each line represents a commit. The 7-character string at the beginning of each line (e.g., a1b2c3d) is a shortened version of the commit hash, which uniquely identifies the commit. You will use these hashes in the next steps to inspect specific commits.

Viewing a Specific Commit's Changes

Now that you have a list of commits, you can use the git show command to view the details and changes of a specific commit. This command shows the commit's metadata (author, date, message) and the "diff," which highlights the exact lines that were added or removed.

Let's inspect the second commit, which has the message "Update README with project description". Find its commit hash from the output of the previous step.

Copy the hash and use it with git show. Replace <commit-hash> with the actual hash from your terminal.

git show <commit-hash>

For example, if the hash for that commit was e4f5g6h, you would run:

git show e4f5g6h

The output will look something like this:

commit e4f5g6h1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7
Author: LabEx <labex@example.com>
Date:   ...

    Update README with project description

diff --git a/README.md b/README.md
index ...
--- a/README.md
+++ b/README.md
@@ -1 +1,3 @@
 ## My Project
+
+This is a simple project to demonstrate Git.

Let's break down the output:

  • Commit Metadata: The first few lines show the full commit hash, author, date, and the commit message.
  • Diff Section: The part starting with diff --git is the "diff".
    • --- a/README.md and +++ b/README.md indicate the "before" and "after" versions of the file.
    • Lines prefixed with + are lines that were added in this commit.
    • Lines prefixed with - (not present in this example) are lines that were removed.

Viewing Changes for a Specific File in a Commit

Sometimes, a commit might change multiple files, but you are only interested in the changes to one specific file. You can tell git show to only display the changes for a particular file by adding the file path to the end of the command.

Let's look at the most recent commit, which has the message "Add application file". This commit added the app.py file.

First, get the hash for the latest commit from your git log --oneline output. Then, run git show with that hash, followed by -- and the filename app.py.

git show app.py < latest-commit-hash > --

For example, if the latest commit hash is a1b2c3d, the command would be:

git show a1b2c3d -- app.py

The output will now be limited to the changes made to app.py in that commit.

commit a1b2c3d...
Author: LabEx <labex@example.com>
Date:   ...

    Add application file

diff --git a/app.py b/app.py
new file mode 100644
index 0000000..d95f32b
--- /dev/null
+++ b/app.py
@@ -0,0 +1 @@
+print("Hello, Git!")

Notice that the output is much shorter and only contains the diff for app.py. The line new file mode 100644 indicates that this file was created in this commit.

Comparing Two Commits

While git show is for viewing the changes within a single commit, you can use git diff to see the cumulative changes between any two commits. This is useful for seeing everything that has changed between two points in time, like between two releases.

Let's compare the initial commit and the latest commit to see all the changes made in the project so far. You will need the commit hashes for the first commit ("Initial commit: Add README.md") and the last commit ("Add application file").

Use the git diff command with the two hashes.

git diff <first-commit-hash> <last-commit-hash>

For example, if your first commit hash is i7j8k9l and your last is a1b2c3d:

git diff i7j8k9l a1b2c3d

The output will show a combined diff of all changes that occurred between these two commits.

diff --git a/README.md b/README.md
index ...
--- a/README.md
+++ b/README.md
@@ -1 +1,3 @@
 ## My Project
+
+This is a simple project to demonstrate Git.
diff --git a/app.py b/app.py
new file mode 100644
index 0000000..d95f32b
--- /dev/null
+++ b/app.py
@@ -0,0 +1 @@
+print("Hello, Git!")

This output shows both the update to README.md and the creation of app.py, giving you a complete picture of the project's evolution between these two points.

Summary

In this lab, you have learned how to navigate a Git repository's history and inspect changes. You practiced using git log to list commits, git show to view the details and changes within a single commit (both for the entire commit and for a specific file), and git diff to compare the differences between two commits. These commands are fundamental for understanding code changes, debugging, and collaborating effectively in any Git-based project.