How do I show the changes which have been staged?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Staged changes in Git are the exact contents of your next commit, not just a list of files you touched. Reviewing that staged snapshot before committing is one of the easiest ways to catch accidental file additions, partial staging mistakes, and commits that mix unrelated work.
Understand What “Staged” Means
Git works with three important states:
- the working tree, which is what is on disk right now
- the index, which holds the staged content for the next commit
HEAD, which is the last committed snapshot
When you ask to see staged changes, you are asking Git to compare the index against HEAD. That is why the correct command is not plain git diff.
Use git diff --cached
The standard command is:
You can also use:
In modern Git, these are equivalent. They show the patch that would become a commit if you ran git commit immediately.
If you want a quick summary before reading the full patch, these are useful:
These summary views help catch accidental file additions or large changes before you inspect the full diff in detail.
Know the Difference Between Staged and Unstaged Diffs
These commands answer different questions:
- '
git diffshows working tree versus index' - '
git diff --cachedshows index versusHEAD' - '
git diff HEADshows all uncommitted changes versusHEAD'
Example:
If both commands show output, you have partially staged work. That is normal and often useful, but you should understand it before committing.
Review One Path at a Time
If the staged patch is large, narrow the diff to one file or directory:
This makes staged review manageable during bigger refactors and helps keep the commit focused.
Use Patch Mode for Precise Staging
When one file contains several unrelated edits, patch mode is the best way to stage only the hunks that belong in the next commit.
After staging interactively, always inspect the staged diff again. That confirms the index contains exactly what you meant to commit, not just what you happened to select quickly.
Unstage Mistakes Without Losing Work
If you staged the wrong file or the wrong hunk, remove it from the index while keeping the working tree changes.
To unstage everything:
This is much safer than trying to reconstruct the working tree from memory after an accidental git add ..
Build a Pre-Commit Routine
A simple review routine catches most staging mistakes:
- run
git status -sb - inspect unstaged work with
git diff - inspect commit-ready work with
git diff --cached - run tests or linters
- commit with a focused message
This takes very little time compared with the time lost cleaning up a bad commit later.
Common Pitfalls
The biggest mistake is running git diff and assuming it shows staged content. Another is using git add . and committing immediately without checking what actually entered the index.
Developers also forget that one file can be both staged and unstaged at the same time, which makes partial-state mistakes easy to miss.
Summary
- Use
git diff --cachedorgit diff --stagedto inspect what the next commit will contain. - Keep working tree, index, and
HEADconceptually separate. - Use summary views for quick checks and full patch views for accuracy.
- Unstage mistakes with
git restore --stagedwithout losing the underlying edits. - Make staged-diff review part of every normal commit workflow.
Related reading
- How do I squash my last N commits together?
- How do I squash two non-consecutive commits?
- How do I stash only one file out of multiple files that have changed?
- How do I sync the SVN revision number with my ASP.NET web site?
- How do I tell git-svn about a remote branch created after I fetched the repo?
- How do I un-revert a reverted Git commit?
- How do I un-revert a reverted Git commit?
- How do I undo 'git add' before commit?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseInterview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.