Version comparison is a fundamental and commonly used operation in Git. Just as we check the changes before and after modifying an article, Git helps us track code changes between different versions through the diff (difference) tool. Whether you want to know what changes were made in a specific commit or compare the code differences between two branches, learning version comparison will make collaboration and code management clearer.

Why Version Comparison?

Imagine you are developing a feature and have modified a lot of code but are unsure where the error occurred. At this point, by comparing the “before modification” and “after modification” versions, you can quickly locate the issue. Common scenarios include:
- Before committing code, check if the contents of the staging area meet expectations;
- Before merging branches, understand the code differences between the two branches;
- When rolling back an erroneous commit, confirm exactly what content needs to be undone.

Common Git Commands and Operations

The core Git command for version comparison is git diff, which can compare code changes across different stages. Here are specific usage scenarios:

1. Compare Working Directory and Staging Area

If you have modified a file but haven’t executed git add yet (i.e., the file is in a “modified but not staged” state), use git diff to see exactly what changed.

Example Operation:
Suppose you have a main.py file with the initial content:

# Initial content
print("Hello, World!")

Now you modify it to:

# After modification
print("Hello, Git!")
print("This is a test.")

Executing git diff at this point will output:

- print("Hello, World!")
+ print("Hello, Git!")
+ print("This is a test.")
  • Lines starting with - are deleted old content (the original print("Hello, World!"));
  • Lines starting with + are added new content (the two new lines).

2. Compare Staging Area and Most Recent Commit

If you have executed git add (the file is in the staging area) and want to check if the staged content is correct before committing, use git diff --staged (or --cached, which has the same effect).

Example Operation:
Continuing from the previous example, after executing git add main.py, run git diff --staged. The output will show the difference between the staging area and the most recent commit (if no commit has been made yet, it defaults to comparing with the “empty version”). Assuming this is the first commit, the staging area is in a “to-be-committed” state, and git diff --staged will show the difference with the most recent commit (i.e., an empty commit):

+ print("Hello, Git!")
+ print("This is a test.")

(If it is the first commit, this will show all staged content because there was no prior commit record, effectively comparing with the “empty version”.)

3. Compare Two Historical Commits

To view code changes between two previous commits, use commit IDs or branch names to specify the two versions.

  • Compare with commit IDs: First, check the commit history with git log and copy the hash values (the first 7-8 characters are sufficient) of the two commits.
    Example Operation:
    Execute git log, and the output (simplified) might look like:
  commit a1b2c3d (HEAD -> main)
    First commit: Add print statement
  commit e4f5g6h
    Second commit: Modify print content

To compare the changes between the second commit (e4f5g6h) and the first commit (a1b2c3d), run:

  git diff e4f5g6h a1b2c3d

The output will show all file changes between these two commits.

  • Compare with relative commit names: If you don’t remember the exact commit IDs, use HEAD (the latest commit), HEAD^ (the parent commit of HEAD), or HEAD~n (the nth most recent commit).
    For example:
  • git diff HEAD~1 HEAD: Compare the most recent commit (HEAD) with the previous commit (HEAD~1);
  • git diff HEAD~2 HEAD~1: Compare the differences between the most recent two commits and the most recent one.

4. Compare Two Branches

Before merging branches (e.g., merging dev into main), use git diff branch1 branch2 to preview the code differences between the two branches.

Example Operation:
If you have two branches, main and dev, and want to compare their differences:

git diff main dev

The output will show all additions, deletions, or modifications between the two branches across all files.

5. Quickly View Complete Content of a Single Commit

To quickly see which files and lines a specific commit modified, use git show <commit-id>.

Example Operation:

git show a1b2c3d  # View the content of the commit with hash a1b2c3d

The output will include the commit information (e.g., author, time, commit message) and all files/line changes made in that commit.

Graphical Tools (Suitable for Beginners)

If command-line operations seem complex, use graphical tools to view version comparisons:
- VS Code: Open the project, click the “Source Control” icon on the left to intuitively see file modifications and staging status, and expand to view specific line-by-line differences.
- GitKraken/SourceTree: Professional Git GUI tools with a more user-friendly interface, supporting branch visualization, commit history trees, and other features.

Summary

Version comparison is a core part of Git’s basic operations. Through the git diff series of commands, you can easily view code changes in the working directory, staging area, historical commits, and between branches. Remember these key points:
- git diff: Working directory vs. staging area;
- git diff --staged: Staging area vs. most recent commit;
- git diff <commit1> <commit2>: Differences between two historical commits;
- git diff branch1 branch2: Differences between two branches.

Practice these commands a few times, and you’ll master version comparison techniques to manage your code more efficiently!

Xiaoye