How can I see what has changed in a file before committing to git?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Before committing, you usually want to answer one precise question: what changed in this file compared with the last commit or compared with what is staged. Git gives you direct commands for each comparison, and using the right one avoids committing accidental edits. The key is to distinguish between unstaged changes, staged changes, and the full difference from HEAD.
See Unstaged Changes in One File
If you modified a file but have not staged it yet, compare the working tree against the index:
That shows only the unstaged edits for that path.
Example:
This is usually the first command to run while reviewing an edit before staging it.
See Staged Changes in One File
If the file is already staged, compare the index against the last commit:
Example:
This answers the more important pre-commit question: what is actually going into the next commit.
See All Changes in the File Relative to HEAD
Sometimes you want both staged and unstaged changes together. Compare the file directly against the last commit:
That shows the total difference between the working tree and the committed version, regardless of staging state.
This is helpful when you have partially staged the file and want a single combined review.
Use a More Readable Summary
If full patch output is too noisy, a summary view can be enough.
Or for staged changes:
This is useful when you want a quick sense of how large the change is before opening the full diff.
Review Changes Interactively
If a file contains both intended and unintended edits, interactive staging helps isolate what should be committed.
After that, review the staged result:
This workflow is often better than trying to read one huge diff and mentally separate commit-worthy hunks from experimental ones.
Use git difftool If You Prefer a Visual Diff
If plain terminal patches are hard to scan, Git can launch a visual diff tool.
For staged changes:
This depends on your Git difftool configuration, but it can be more comfortable for large refactors.
Check File Status Alongside the Diff
A diff tells you content changes. git status tells you whether the file is staged, unstaged, renamed, or untracked.
For example:
- '
Min the right column means unstaged modifications' - '
Min the left column means staged modifications' - both columns set means the file has both staged and unstaged changes
Reading diff output without checking status can be misleading when the file is partially staged.
A Practical Pre-Commit Workflow
A clean review sequence for one file is:
- check status
- inspect unstaged changes
- stage intentionally
- inspect staged changes
Example:
This reduces the risk of surprise commits.
Common Pitfalls
The biggest mistake is using git diff after staging and assuming it shows what will be committed. Plain git diff shows unstaged changes, not the staged snapshot.
Another issue is forgetting the -- path separator, especially when a file path could be confused with a branch or revision name.
Developers also sometimes review only the total git diff HEAD -- file and miss the fact that only part of that change is actually staged.
Summary
- Use
git diff -- path/to/filefor unstaged changes. - Use
git diff --cached -- path/to/filefor what is staged to commit. - Use
git diff HEAD -- path/to/filefor the total change relative to the last commit. - Combine
git status --shortwith diff commands so you understand the file state. - Use patch staging or a visual difftool when one file contains mixed-intent edits.
Related reading
- How can I see what I am about to push with git?
- How can I see which Git branches are tracking which remote / upstream branch?
- How can I see which Git branches are tracking which remote / upstream branch?
- How can I selectively merge or pick changes from another branch in Git?
- How can I set up an editor to work with Git on Windows?
- How can I show what a commit did?
- How can I solve fatal Invalid credentials error when pushing to Bitbucket?
- How can I specify a branch/tag when adding a Git submodule?
.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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.