Git Alias to Diff Staged Changes
Create a git alias for viewing staged changes before committing. Review exactly what will be included in your next commit with a single command.
Detailed Explanation
Diff Staged Changes
The dc alias shows the diff of staged (cached) changes, letting you review exactly what will go into your next commit:
[alias]
dc = diff --cached
Staged vs Unstaged Diffs
Git maintains three trees that determine what different diff commands show:
Working Directory --> Staging Area --> Repository (HEAD)
| | |
|---- git diff ----->| |
| |---- git dc ------->|
|------------ git diff HEAD ------------->|
| Command | Compares |
|---|---|
git diff |
Working directory vs staging area (unstaged changes) |
git dc |
Staging area vs HEAD (staged changes, what will be committed) |
git diff HEAD |
Working directory vs HEAD (all changes since last commit) |
Why This Matters
Before committing, you should always know exactly what you are about to commit. The dc alias makes this a quick one-command check.
Additional Diff Aliases
Build a complete diff toolkit:
[alias]
dc = diff --cached
dw = diff --word-diff
dcw = diff --cached --word-diff
dns = diff --name-status
dcns = diff --cached --name-status
--word-diff highlights changes at the word level rather than line level, which is excellent for prose, documentation, and variable name changes.
--name-status shows only the file names and their status (Modified, Added, Deleted) without the full diff. This is useful when you just want to know which files changed.
Stat Summary
For a middle ground between full diff and just file names:
[alias]
dcs = diff --cached --stat
This shows the diffstat with insertions and deletions per file, giving you a quick sense of the scope of your staged changes.
Use Case
Use this alias as part of your pre-commit review workflow. Run it after staging changes and before committing to verify you are committing exactly what you intend. It catches accidentally staged files, debug logging, and forgotten TODO comments.