Git Basics: Reviewing Staged and Unstaged Changes
A file can contain both staged and unstaged edits. Git’s diff commands answer different questions depending on which two versions they compare.
Run these commands in Bash with Git 2.28 or newer installed. Each article starts in a fresh temporary repository; no remote account is needed.
git_practice=$(mktemp -d) || exit
cd "$git_practice" || exit
git init -q -b main
git config user.name 'Practice User'
git config user.email 'practice@example.invalid'
git config commit.gpgsign falseThese settings apply only to this repository; signing is disabled for the exercise. Use your own identity in real projects. The temporary files remain after the exercise. If Git opens a pager, press q to return to the shell.
Prepare three versions
printf 'port=8080\n' > app.conf
git add app.conf
git commit -q -m 'Add application config'
printf 'port=9090\n' > app.conf
git add app.conf
printf 'port=9090\ndebug=true\n' > app.conf
git status --shortMM app.confThe first M means the staged copy differs from the last commit. The second means the working file differs from the staged copy. Here, the port change is staged and the debug setting is still unstaged.
Inspect each comparison
git diff -- app.confThe patch includes this hunk:
@@ -1 +1,2 @@
port=9090
+debug=true
Plain git diff compares the working file against the staging area. The leading space marks an unchanged context line; + marks an added line.
git diff --cached -- app.confThis compares the staging area against HEAD, the current commit:
@@ -1 +1 @@
-port=8080
+port=9090
Only the port change would enter a normal git commit now. --staged is another spelling of --cached.
Review everything since the commit
git diff HEAD -- app.confThis compares the working file directly with HEAD, showing both edits together. These diff commands do not modify files. The -- separates revisions and options from the filename.
Diff output also has file headers and object IDs, omitted from the short hunks above. Untracked files do not appear in ordinary diff output, so check git status too before deciding that there are no changes.
Reference
Acknowledgements
This post was created with assistance from AI.