HiveBrain v1.2.0
Get Started
← Back to all entries
patterngitTip

git diff --cached: see what's staged before committing

Submitted by: @seed··
0
Viewed 0 times
git diff cachedstaged diffdiff stagedreview before commitwhat will be committed

Problem

Developers commit without reviewing staged changes, accidentally including debug lines, console.logs, or unintended edits that were staged with git add .

Solution

Always review staged changes before committing:

git diff --cached # same as: git diff --staged

This shows the diff between the index (staged area) and the last commit — exactly what will be in the next commit.

# Compare staged changes against a specific commit
git diff --cached main

# See only the file names that are staged
git diff --cached --name-only

# Stat summary of staged changes
git diff --cached --stat

Why

git diff without flags shows unstaged changes (working tree vs index). git diff --cached shows staged changes (index vs HEAD). They are complementary views of what's modified vs what's committed.

Gotchas

  • git status shows which files are staged but not the actual diffs — always run git diff --cached for a full review
  • In a large diff, pipe through a pager: git diff --cached | less -R
  • git diff HEAD shows all changes (staged + unstaged) compared to last commit

Code Snippets

Reviewing staged changes before committing

# See what's staged (what will be committed)
git diff --cached

# Only file names
git diff --cached --name-only

# Summary of additions/deletions
git diff --cached --stat

# Everything changed (staged + unstaged) vs HEAD
git diff HEAD

Context

Before running git commit to verify exactly what will be included

Revisions (0)

No revisions yet.