git diff
View the exact changes between commits, branches, or the working tree
Practical Example
git diff # unstaged changes vs staging git diff --staged # staged vs last commit git diff main feature-login # compare two branches
Command Overview & How It Works
Compares different states in the repository and shows the line-by-line differences. Lines starting with + are additions (shown in green), lines starting with - are deletions (shown in red). By default, git diff compares the working directory against the staging area. Use git diff --staged (or --cached) to see what is staged versus the last commit. Use git diff <branch1> <branch2> to compare two branches, or git diff <commit1> <commit2> to compare specific commits. Add --stat for a summary of changed files and line counts, --name-only to show only file names, or --color-words for a word-level (instead of line-level) diff. For complex merges, git diff --ours, --theirs, and --base help identify conflicting changes during merge conflicts.
Practical Tips
- •Run git diff --staged before committing to review exactly what is about to be recorded.
- •Use git diff --stat for a quick summary of which files changed and by how many lines.
- •For code reviews, git diff main...feature shows only the changes made on the feature branch since it diverged.