git diff, git diff --cached, git diff --staged, git diff HEAD
git diff [<options>] [<commit>] [--] [<path>...]
git diff [<options>] --cached [--merge-base] [<commit>] [--] [<path>...]
git diff --stagedCompare the working tree with the index, the index with HEAD, or the working tree with HEAD.
| Parameter | Description |
|---|---|
--cached / --staged | Compare the index with HEAD, or with the given commit. The two option names are aliases. |
HEAD | Compare the working tree with the current commit, including staged and unstaged file contents. |
--unified=<n> | Patch context lines. The example uses 0 to keep the output short and stable. |
Returns: A patch or a name-status list on standard output. In a repository, the default exit status is 0 even when a patch is printed.
Exit status
0— The default git diff porcelain in a repository exits 0 after printing differences. This was measured on Git 2.39.5.1— Returned when --exit-code or --quiet is used and the trees differ. The example prints that as plain-quiet-exit=1.
- Inspect both the plain and the cached diffs before you commit. An empty plain diff can still hide staged changes.
git init -b main >/dev/null
git config user.name "Practice Author"
git config user.email "[email protected]"
git config commit.gpgsign false
git config color.ui false
mkdir -p .disabled-hooks
git config core.hooksPath ".disabled-hooks"
printf 'one\n' > file.txt
git add file.txt
git commit -m "Add file" >/dev/null
printf 'one\ntwo\n' > file.txt
git add file.txt
printf 'one\ntwo\nthree\n' > file.txt
echo "plain"
git diff --unified=0
git diff --quiet
echo "plain-quiet-exit=$?"
echo "cached"
git diff --cached --unified=0
cached=$(git diff --cached)
staged=$(git diff --staged)
if [ "$cached" = "$staged" ]; then echo "cached-equals-staged=yes"; else echo "cached-equals-staged=no"; fi
echo "HEAD-name-status"
git diff HEAD --name-status
Expected output
plain
diff --git a/file.txt b/file.txt
index 814f4a4..4cb29ea 100644
--- a/file.txt
+++ b/file.txt
@@ -2,0 +3 @@ two
+three
plain-quiet-exit=1
cached
diff --git a/file.txt b/file.txt
index 5626abf..814f4a4 100644
--- a/file.txt
+++ b/file.txt
@@ -1,0 +2 @@ one
+two
cached-equals-staged=yes
HEAD-name-status
M file.txt
Related: git status, Git tutorial: Inspect changes and history