Git Diff Basics
What is Git Diff?
Git diff is a powerful command-line tool that allows developers to compare changes between different states of a Git repository. It helps track modifications in files, compare different versions of code, and understand the exact differences between commits, branches, or working directory and staging area.
Basic Diff Operations
Comparing Working Directory and Staging Area
git diff
This command shows unstaged changes in your working directory compared to the staging area.
Comparing Staged Changes
git diff --staged
Displays changes that have been staged but not yet committed.
Diff Workflow Visualization
graph TD
A[Working Directory] -->|Changes| B[Staging Area]
B -->|git diff| C[Uncommitted Changes]
B -->|git diff --staged| D[Staged Changes]
D -->|Commit| E[Repository]
Diff Output Interpretation
Symbol |
Meaning |
+ |
Added lines |
- |
Removed lines |
Highlighted text |
Modified content |
Advanced Diff Options
Comparing Specific Files
git diff path/to/file
Comparing Branches
git diff branch1..branch2
Best Practices
- Always review diff output before committing
- Use diff to understand code changes
- Leverage diff for code review processes
At LabEx, we recommend mastering Git diff as a crucial skill for effective version control and collaborative development.