KKAIROS
Practical guideIllustrative3 min read

git diff: unstaged, staged, and against a commit

git diff compares the working tree with the index; git diff --cached compares the index with HEAD. TE-12-A shows both. TE-12-B shows --stat and TE-12-C shows --exit-code with --quiet. No other form is tested.

Published:

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

TE-12-A: git diff shows unstaged changes only; --cached shows staged changes

Setup for this run, inside a throwaway directory (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'one\n' > f.txt; git add f.txt; git commit -qm one
printf 'two\n' > f.txt; git add f.txt

Command:

echo plain=$(git diff | wc -l); echo cached=$(git diff --cached | wc -l)

Recorded output:

plain=0
cached=7

TE-12-B: git diff --stat summarizes an unstaged change as one file with one insertion and one deletion

Setup for this run, inside a throwaway directory (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'one\n' > f.txt; git add f.txt; git commit -qm one
printf 'two\n' > f.txt

Command:

git diff --stat

Recorded output:

f.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Exit status: 0. This status and the output match what was written before the run.

TE-12-C: expected non-zero exit: git diff --exit-code --quiet exits with status 1 when there is a difference

Setup for this run, inside a throwaway directory (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'one\n' > f.txt; git add f.txt; git commit -qm one
printf 'two\n' > f.txt

Command:

git diff --exit-code --quiet

Recorded output: none, standard output was empty.

Exit status: 1. This status and the output match what was written before the run.

TE-12-B: by the setup, f.txt changed from one to two without being staged. git diff --stat printed f.txt | 2 +-, then 1 file changed, 1 insertion(+), 1 deletion(-). TE-12-C: git diff --exit-code --quiet printed nothing and exited with status 1, and the same change was still unstaged by the setup.

Limits

The wrong form may show nothing or a different change set.

Ad

Source

Evidence

Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-12-A | git diff shows unstaged changes only; --cached shows staged changes | 0 | PASS | 4feb3d84ccf6 | | TE-12-B | git diff --stat summarizes an unstaged change as one file with one insertion and one deletion | 0 | PASS | f9acadefafe3 | | TE-12-C | expected non-zero exit: git diff --exit-code --quiet exits with status 1 when there is a difference | 1 | PASS | 6cc59c881bd2 |

Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.

All full guides: All guides