What is git diff used for?
git diff is used to view differences (changes) between file versions: before a commit, after a commit, or between commits/branches.
Simply put: the command shows exactly what changed, not just that something changed.
What git diff shows by default
bash
git diffShows:
- differences between the working directory and the staging area
- changes that have not yet been added via
git add
Main use cases
Difference between the staging area and the last commit
bash
git diff --staged
# or
git diff --cachedAll changes (both staged and unstaged)
bash
git diff HEADDifference between two commits
bash
git diff commit1 commit2Difference between branches
bash
git diff main feature/loginChanges in a specific file
bash
git diff src/app.jsHow to read git diff output
diff
- const a = 1;
+ const a = 2;-- a removed line+- an added line- no sign - context
Why git diff is used in practice
- Check code before
git add - Deliberately shape commits
- Find the source of a bug
- Do a code review locally
- Compare implementations
Frequent mistakes
- Confusing
git diffandgit status - Not checking changes before a commit
- Committing "blindly"
Short version for an interview
git diffis used to view differences between file versions - in the working directory, staging area, commits, or branches.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.