Git
How to Use git diff in Git?
The git diff
command is a powerful tool in Git that allows developers to view differences between files, branches, commits, and working states. By examining these differences, you can better understand changes, track modifications, and review updates before making new commits. Whether you’re working solo or as part of a team, understanding git diff
is essential to managing changes effectively.
In this blog, we’ll cover how to use git diff
in various scenarios, along with practical examples to help you get the most out of this command.
Why Use git diff
?
Git diff
provides insight into:
- Code Changes: See the exact lines that were modified, added, or deleted.
- Unstaged vs. Staged Changes: Compare working files with what’s staged for commit.
- Branch Differences: Compare code across different branches.
- Commit Changes: Understand specific modifications in individual commits.
This command makes it easy to see what’s been altered, helping you identify potential issues and verify the correctness of changes before committing or merging.
Basic Usage of git diff
The git diff
command without any arguments displays changes in files that haven’t been staged yet.
git diff
This shows a line-by-line difference of all modified files since the last commit. You’ll see indicators like:
+
: Lines added-
: Lines removed
For example:
- console.log("Hello, World!");
+ console.log("Hello, Universe!");
Viewing Staged Changes with git diff --cached
To view differences in files that have been staged (added) but not yet committed, use git diff --cached
:
git diff --cached
This displays changes in the staging area, giving you a final chance to review before committing.
Comparing Specific Files
If you want to view changes in a specific file, you can specify the file name directly:
git diff <filename>
For example:
git diff src/app.js
This command shows the differences only in the app.js
file.
Comparing Different Branches
To compare the differences between two branches, you can use the branch names directly:
git diff <branch1> <branch2>
For example, to see differences between the main
branch and a feature branch:
git diff main feature-branch
This is particularly useful when you want to see what changes will be merged or identify conflicting updates across branches.
Comparing Changes Between Commits
You can also use git diff
to compare changes between two specific commits by referencing their commit hashes:
git diff <commit1> <commit2>
For example:
git diff 8d3a3b7 4f5c6a1
This command shows changes between the two specified commits. It’s helpful when you want to understand how code evolved over time or examine specific updates.
Viewing Changes from a Specific Commit
If you’d like to view changes introduced in a single commit, you can use the commit hash with git diff
along with the ^
symbol to indicate comparison with the previous commit:
git diff <commit>^ <commit>
For example:
git diff 8d3a3b7^ 8d3a3b7
Alternatively, Git provides a shorthand using git show
:
git show <commit>
This displays the changes for a specific commit along with metadata such as the author and commit message.
Comparing the Working Directory with a Commit
To see differences between the current working directory and a specific commit, use:
git diff <commit>
This command is useful when you want to see what modifications have been made since a certain point in the project’s history.
Checking Differences Between Staged Changes and a Commit
If you want to compare the staged changes with a particular commit, use:
git diff --cached <commit>
This command helps you verify that only the intended changes have been staged, which is especially helpful before making a final commit.
Using git diff
with Paths
You can limit git diff
to show differences only in a specific directory or set of files. This is useful when working with large codebases or when changes are concentrated in particular areas of the project.
For example, to compare differences only within the src
directory:
git diff main feature-branch -- src/
This command will show differences between branches main
and feature-branch
but only for files within the src
folder.
Viewing Word-Level Differences
By default, git diff
shows line-by-line differences, but you can also view word-level differences for a more granular view. Use the --word-diff
option:
git diff --word-diff
With this option, git diff
highlights specific word changes within lines, which can be helpful for identifying minor edits, such as changes to variable names.
Additional Useful Options for git diff
--color-words
: Highlights only the words that have changed, making it easier to read the differences without full line-by-line details.
git diff --color-words
--stat
: Shows a summary of changes instead of full details. It includes the number of files changed, the number of insertions and deletions, and a breakdown by file.
git diff --stat
-U<number>
: Specifies the number of lines of context around changes. For instance,-U3
will show three lines of context around each change.
git diff -U3
Best Practices When Using git diff
- Regularly Check Changes: Use
git diff
frequently to review modifications, ensuring you’re aware of the changes you’re introducing before staging or committing them. - Use
--stat
for Summaries: When working with large files,--stat
provides a quick summary, saving time while giving a high-level view. - Combine with
git status
: Pairgit diff
withgit status
to see which files have been modified and quickly identify changes. - Test Before Committing: Review changes with
git diff --cached
to double-check staged modifications, ensuring accuracy before the final commit.
Conclusion
The git diff
command is an essential tool for any developer working with Git. By mastering its various options and knowing when to use each one, you can gain greater control over your code, catch potential issues early, and collaborate more effectively with your team. Whether you’re comparing branches, commits, or individual files, git diff
empowers you to review changes confidently and ensure quality in your projects.