Understanding and Utilizing Git Diff
Git is a powerful version control system, and understanding how to examine changes between commits is fundamental to effective collaboration and project management. The git diff
command is your primary tool for this task. This tutorial will guide you through the various ways to use git diff
to inspect changes in your Git repository.
What Does git diff
Do?
At its core, git diff
shows the differences between commits, branches, files, and even your working directory. It highlights what has been added, deleted, or modified. The output is presented in a “patch” format, which is a standardized way of describing changes in a text file.
Basic Usage: Comparing Your Working Directory
The simplest use of git diff
compares your current working directory (the files you’re actively editing) to the staging area (also known as the index).
git diff
This will show you the changes you’ve made but haven’t yet staged for commit. Any additions will be marked with a +
prefix, and deletions with a -
prefix.
Comparing Staged Changes
To see the changes you have staged for commit (the difference between the staging area and the last commit), use the --staged
option (or --cached
, which is an alias):
git diff --staged
This is useful to review what will be included in your next commit before you actually commit it.
Comparing Commits
The power of git diff
truly shines when comparing commits. To see the differences between two commits, specify their commit hashes (SHA-1 identifiers).
git diff <commit_hash_1> <commit_hash_2>
This command displays the changes that were made between <commit_hash_1>
and <commit_hash_2>
. Remember that the order matters: the diff shows what changes were made to get from the first commit to the second commit.
Important Note on Commit Ranges: You can also specify a commit range using the ..
notation.
git diff <commit_hash_1>..<commit_hash_2>
This is often used to see all the changes introduced between two commits. This displays a cumulative diff of all commits in the specified range.
To illustrate: If you have commits A, B, and C, and you run git diff A..C
, you’ll see the combined changes that were made in commits B and C to get from A to C.
Parent Commits and Understanding ^
Sometimes you want to see the changes introduced by a specific commit rather than the changes between two commits. This is where understanding parent commits and the ^
symbol becomes crucial.
Consider a commit X
. X^
refers to the first parent commit of X
. This is particularly useful when you want to see what changes were introduced by X
relative to its predecessor.
For example, if you want to see the changes introduced by a commit k73ud
compared to its parent, you can use:
git diff k73ud^..k73ud
This effectively isolates the changes made in k73ud
.
Comparing with HEAD
HEAD
is a pointer to the latest commit on your current branch. You can use it with git diff
to compare your working directory or staging area to the latest commit.
git diff HEAD
: Compares your working directory to the latest commit.git diff --staged HEAD
: Compares your staging area to the latest commit.
Saving the Diff to a File
Sometimes you need to share or store the diff for later use. You can redirect the output of git diff
to a file using the >
operator.
git diff <commit_hash_1>..<commit_hash_2> > my_changes.patch
This creates a file named my_changes.patch
containing the diff. This patch file can be applied to another repository using the git apply
command.
Useful Options
--name-only
: Displays only the names of the files that have changed.--stat
: Displays a summary of the changes, including the number of files changed, insertions, and deletions.--color-words
: Highlights changes within lines instead of entire lines, making it easier to see small modifications.