Git provides powerful tools to compare changes between commits, helping developers understand what has been modified, added, or deleted. The git diff command is the primary tool for viewing differences between commits, files, branches, or even the working directory and the staging area. Below, we explain how to use git diff to view differences between commits, along with examples.
1. View Differences Between Two Commits
To compare changes between two specific commits, use the git diff command followed by the commit hashes.
Example: Compare Two Commits
git diff abc123 def456
Here, abc123 and def456 are the commit hashes. The output shows the differences between the two commits.
2. View Differences Between the Working Directory and the Last Commit
To see the changes in your working directory that have not been staged for the next commit, use git diff without any arguments.
Example: Compare Working Directory with Last Commit
git diff
3. View Differences Between the Staging Area and the Last Commit
To see the changes that have been staged for the next commit but not yet committed, use git diff --cached.
Example: Compare Staging Area with Last Commit
git diff --cached
4. View Differences Between Two Branches
To compare the changes between two branches, use git diff followed by the branch names.
Example: Compare Two Branches
git diff main feature-branch
This command shows the differences between the main branch and the feature-branch.
5. View Differences for a Specific File
To compare changes for a specific file between two commits or branches, specify the file path after the commit hashes or branch names.
Example: Compare a Specific File Between Two Commits
git diff abc123 def456 path/to/file.txt
Example: Compare a Specific File Between Two Branches
git diff main feature-branch path/to/file.txt
6. View Differences in a Summary Format
To see a summary of changes (e.g., which files were modified, added, or deleted) without the detailed diff, use the --stat option.
Example: Summary of Changes Between Two Commits
git diff --stat abc123 def456
7. View Differences with Context Lines
To include a specific number of context lines around the changes, use the -U option followed by the number of lines.
Example: Show Differences with 5 Context Lines
git diff -U5 abc123 def456
8. View Differences in a Word-by-Word Format
To see differences at the word level rather than the line level, use the --word-diff option.
Example: Word-by-Word Differences
git diff --word-diff abc123 def456
9. View Differences Between the Last Commit and Its Parent
To see the changes introduced by the last commit, use git diff HEAD^ HEAD
This command compares the last commit with its parent, showing what changes were made in the most recent commit.
Conclusion
The git diff command is an essential tool for developers to understand changes in their codebase. By using various options and arguments, you can effectively compare commits, branches, and files, making it easier to track modifications and collaborate with others. Mastering git diff will enhance your ability to manage and review changes in your Git repositories.
