How to view file diff in git before commit
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Reviewing a diff before committing is one of the simplest ways to avoid noisy history and accidental mistakes. Git lets you compare the working tree, the staging area, and the last commit, so you can confirm exactly what will be recorded before you run git commit.
Understand Which Diff You Want
Most confusion comes from not knowing which two states Git is comparing. There are three common views:
- working tree versus staging area
- staging area versus last commit
- working tree versus last commit
The default git diff command shows unstaged changes. That means Git compares the current file on disk with the staged copy in the index.
If you want to limit the output to one file, pass the path:
That is the fastest way to inspect a single file before deciding whether to stage it.
Review What Will Actually Be Committed
After you stage changes with git add, use git diff --cached to review the exact patch that will go into the next commit.
Again, you can narrow the diff to one file:
This distinction matters because a file can have both staged and unstaged edits at the same time. If you only run git diff, you are not necessarily seeing what the next commit contains.
To compare your current working copy against the last commit in one shot, use:
That view includes both staged and unstaged changes relative to HEAD.
Use Better Diff Views
Large patches are easier to understand with a few helpful options.
Word-level changes are useful for prose, configuration, or short line edits:
If rename detection matters, especially during refactors, compare with status as well:
For terminal review, it is also worth enabling a pager and color if your setup does not already do that:
Those are not required, but they make repeated diff review much easier.
Stage Selectively Before Commit
One of the best habits is staging only the parts that belong in the commit you are about to write. Git supports this directly with patch mode:
Patch mode walks through each hunk and lets you decide whether to stage it. That is useful when one file contains both a bug fix and unrelated formatting changes. After staging only the relevant hunks, run git diff --cached and verify that the commit is focused.
A practical workflow looks like this:
That sequence catches most accidental commits before they happen.
Common Pitfalls
The most common mistake is reviewing only git diff and assuming that is the future commit. It only shows unstaged changes. If files are already staged, you also need git diff --cached.
Another issue is forgetting that generated files, lock files, or formatting-only edits may be mixed into the patch. git status is a good first checkpoint because it shows which files are modified, staged, or untracked before you inspect the patch itself.
Developers also sometimes commit from a dirty working tree without realizing one file contains extra debug logging. A focused pre-commit diff review is the cheapest defense against that kind of mistake.
Finally, very large diffs are hard to reason about. If review feels difficult, that is often a signal that the commit should be split into smaller pieces.
Summary
- '
git diffshows unstaged changes.' - '
git diff --cachedshows the staged patch that will be committed.' - '
git diff HEAD -- pathshows all current changes for a file relative to the last commit.' - '
git add -phelps build cleaner, smaller commits by staging selected hunks.' - Reviewing diffs before commit reduces accidental changes and improves commit quality.
Related reading
- How to 'Watch' only a directory in a GitHub repository?
- How would Git handle a SHA-1 collision on a blob?
- How would I extract a single file (or changes to a file) from a git stash?
- How would I extract a single file or changes to a file from a git stash?
- I change the capitalization of a directory and Git doesn't seem to pick up on it
- I ran into a merge conflict. How do I abort the merge?
- if i set value of commit.interval.ms = in kafka stream, Whether it will be able to commit offset?
- ignore all .git folders in .dockerignore
.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.