Q: What's the difference between `git diff`, `git diff --staged`, and `git diff HEAD`? When would you use each?
Git has three important snapshots: the last commit (HEAD), the staging area (index), and the working directory. Each diff variant compare...
#Git #Diffing, Logging & Code Archaeology #L1 #Version Control #Collaboration
🎙️ Candidate Opening & Architectural Context
""Git is an immutable directed acyclic graph (DAG); knowing commands like git reflog means you never truly lose commits. The interviewer is testing: Understanding of the three-tree architecture (working tree, index, HEAD).. I structure my answer around systematic triage first, root cause analysis second, and permanent remediation third.""
Advertisement
🛠️ Production Runbook & Step-by-Step Resolution
1️⃣
Initial Diagnostics & Root Cause Analysis
Git has three important snapshots: the last commit (HEAD), the staging area (index), and the working directory. Each diff variant compares two of these:
git diff— working directory vs staging area. Shows changes you've made but haven't staged yet.git diff --staged(or--cached) — staging area vsHEAD. Shows what will go into the next commit if you rungit commitright now.git diff HEAD— working directory vsHEAD. Shows *all* changes since the last commit, whether staged or not.
2️⃣
Remediation & Permanent Safeguards
Other useful variations: For reviewing what you're about to commit, the workflow is: git diff to review unstaged changes → git add -p to selectively stage → git diff --staged to verify what's staged → git commit.
git diff main..feature # diff between two branches
git diff HEAD~3..HEAD # last 3 commits' changes
git diff --stat # summary (files changed, insertions, deletions)
git diff --name-only # just filenames
git diff -- path/to/file # limit to specific file
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Pro-Tip: git diff — working directory vs staging area. Shows changes you've made but haven't staged yet.."
⚡ 60-Second Elevator Pitch Talking Points
- git diff — working directory vs staging area. Shows changes you've made but haven't staged yet.
- git diff --staged (or --cached) — staging area vs HEAD. Shows what will go into the next commit i...
- git diff HEAD — working directory vs HEAD. Shows *all* changes since the last commit, whether sta...
Advertisement