A diff describes how one version differs from another. Before interpreting its output, identify those two versions: a file in the working directory, a staged snapshot, a committed snapshot, or two ordinary files. Once the comparison is clear, the same output can also become a patch for transferring changes.
The original notes focused on Git's staging area and linked to a Linux diff tutorial. This article joins those topics into a workflow: compare, read the output, save a patch, and apply it with the appropriate tool.
Choose the two versions to compare
For a tracked file in a repository with an existing commit:1
| Command | Old side → new side |
|---|---|
git diff | Index (staging area) → working tree |
git diff --staged | HEAD → index |
git diff HEAD | HEAD → working tree |
git diff A B | Commit A → commit B |
--cached is another name for --staged. Use two ordinary hyphens in the option; a typographic dash copied from prose will not work.
The Refine tutorial collected in the draft captures the first comparison:
“We can also compare our changes in the working directory with the changes added in the staging area.”
— Khabbab (2024), “Git Diff Between staging area and working directory,” para. 1.2
Staging records a snapshot. Editing the same file afterward does not automatically update that snapshot, so a file can have both staged and unstaged changes.
Follow one file through all three states
Run this example in a new practice directory. Git needs a configured author name and email for the initial commit.
mkdir diff-demo
cd diff-demo
git init
printf 'color=blue\nsize=small\n' > settings.txt
git add settings.txt
git commit -m "Add initial settings"
# Change the color and stage that version.
printf 'color=green\nsize=small\n' > settings.txt
git add settings.txt
# Change the size without staging again.
printf 'color=green\nsize=large\n' > settings.txt
There are now three snapshots:
| Location | Color | Size |
|---|---|---|
HEAD | blue | small |
| Index | green | small |
| Working tree | green | large |
git diff -- settings.txt shows only the unstaged size change. Its hunk is:
@@ -1,2 +1,2 @@
color=green
-size=small
+size=large
git diff --staged -- settings.txt shows the staged color change:
@@ -1,2 +1,2 @@
-color=blue
+color=green
size=small
git diff HEAD -- settings.txt shows the net changes to both lines. If you now run an ordinary git commit, only the staged color change is included; the size change remains in the working tree.
Read a unified diff
Above the hunk, Git identifies the old and new paths:
diff --git a/settings.txt b/settings.txt
--- a/settings.txt
+++ b/settings.txt
Inside a hunk, a leading space marks context, - marks a line from the old side that is removed, and + marks a line added on the new side. In @@ -1,2 +1,2 @@, each side starts at line 1 and contains two lines. The counts include context lines.3
Red and green are display conventions for removals and additions, not permanent labels for the staging area and working directory. Their meaning depends on the comparison you selected. The --- and +++ file headers identify paths; they are not removed or added content lines.
Compare commits, branches, and selected files
Find commit IDs and compare their snapshots:
git log --oneline
git diff OLD_COMMIT NEW_COMMIT -- path/to/file
For branches, git diff main topic compares their tips. git diff main...topic compares the merge base with topic; the order matters. git diff main..topic is equivalent to comparing the two tips, unlike the commit-range meaning of .. in git log.1
Useful review options include:
git diff --stat
git diff --name-only
git diff --staged --check
--check checks whitespace errors, not whether a patch can be applied. Ordinary git diff does not list untracked file contents. Check git status for those files, or stage the intended new files before exporting a staged patch.1
Compare ordinary files with Unix diff
diff is a command-line utility, not a shell built-in. It works without a Git repository. For example:
printf 'color=blue\nsize=small\n' > old.txt
printf 'color=green\nsize=small\n' > new.txt
diff -u old.txt new.txt
-u selects unified output. For directories, diff -ru old-dir new-dir compares recursively; adding -N treats missing files as empty for the comparison. diff -q old.txt new.txt only reports whether they differ.3
GNU diff returns 0 for identical input, 1 for differences, and 2 for an error. A patch-producing command may therefore succeed in its purpose while returning 1. In a script using set -e, handle that result explicitly rather than hiding all failures with || true.3
Git can also compare files outside a repository:
git diff --no-index -- old.txt new.txt
This form also returns 1 when differences are found.1 The Linux diff tutorial linked in the draft provides further examples of normal, context, and unified output.4
Export and apply a plain Git patch
Choose the snapshot pair deliberately. These are alternatives:
# Unstaged changes, relative to the index:
git diff --binary --no-ext-diff --no-textconv > ../unstaged.patch
# Staged changes, relative to HEAD:
git diff --staged --binary --no-ext-diff --no-textconv > ../staged.patch
# Net tracked changes, relative to HEAD:
git diff HEAD --binary --no-ext-diff --no-textconv > ../working-tree.patch
The first patch expects the staged version as its starting point. It may not apply to a checkout of HEAD if that index already contains other changes. The second is appropriate for transferring exactly what is staged. Untracked files are not automatically included in the third.
--binary includes applicable binary changes. Disabling external diff drivers and text conversion avoids exporting presentation-only output as an application patch.1
In a target checkout containing the expected base files, run from the repository root:
git apply --stat ../staged.patch
git apply --check ../staged.patch
git apply ../staged.patch
git diff
The first two commands inspect without applying. Plain git apply changes working files; it does not stage or commit them. The manual states:
“This command applies the patch but does not create a commit.”
— Git, git-apply, “Description,” para. 2.5
To update both index and working tree, use git apply --check --index followed by git apply --index. This requires the relevant index and working-tree copies to match. --cached applies only to the index.5
By default, an unapplicable hunk makes git apply reject the whole patch. --reject changes this to partial application with .rej files. Use it only when deliberately resolving a partial result.5
Apply a Unix patch and understand path stripping
Using the old.txt and new.txt files above, generate a patch with explicit target paths:
diff -u --label a/settings.txt --label b/settings.txt old.txt new.txt > settings.patch
The expected exit status is 1, because the files differ. In a target directory whose settings.txt contains the old version:
patch --dry-run -p1 < /path/to/settings.patch
patch -p1 < /path/to/settings.patch
GNU's description of --dry-run is explicit:
“Print the results of applying the patches without actually changing any files.”
— GNU Project, “patch Options,”
--dry-runentry.6
-p1 strips the first path component, turning a/settings.txt into settings.txt. A patch whose header already contains just settings.txt normally calls for -p0. Check the headers and target directory rather than guessing.6
GNU patch can apply successful hunks while rejecting others, and may use offsets or fuzzy matching. Read its diagnostics and inspect the result. Text patches can sometimes be exchanged between patch and git apply, but Git-specific binary patches and metadata should be handled with Git's tools.35
Transfer commits with format-patch and am
A plain diff carries file changes, not a complete commit record. To transfer a series of ordinary commits together with their authorship and messages, use git format-patch:7
# Export the latest commit as a numbered patch file.
git format-patch -1 HEAD -o ../patch-series
# Alternatively, export commits on topic that are not on main.
git format-patch main..topic -o ../patch-series
Use a fresh output directory for each export so unrelated patch files are not mixed into the series. In the target repository, with a clean working tree and an appropriate base:
git am ../patch-series/*.patch
Unlike git apply, git am creates commits. It preserves the supplied author information and commit message, while the committer is the person applying the series. It is not a guarantee of identical commit IDs or preservation of a merge graph.8
If application stops, resolve the files and stage the result, then run git am --continue. Use git am --abort to abandon the operation. A raw git diff file is not the mail-formatted commit series expected by this workflow.8
References
Footnotes
-
Git. (n.d.). git-diff. Retrieved February 19, 2025, from https://git-scm.com/docs/git-diff ↩ ↩2 ↩3 ↩4 ↩5
-
Khabbab, M. (2024, July 29). git diff - Comparing changes in Git. [Blog post]. Refine. https://refine.dev/blog/git-diff-command/#git-diff-best-practices ↩
-
GNU Project. (n.d.). Comparing and merging files. GNU Diffutils manual. Retrieved February 19, 2025, from https://www.gnu.org/software/diffutils/manual/diffutils.html ↩ ↩2 ↩3 ↩4
-
Zivanov, S. (2024, July 18). diff command in Linux: Syntax, options, examples. phoenixNAP. https://phoenixnap.com/kb/linux-diff ↩
-
Git. (n.d.). git-apply. Retrieved February 19, 2025, from https://git-scm.com/docs/git-apply ↩ ↩2 ↩3 ↩4
-
GNU Project. (n.d.). patch options. GNU Diffutils manual. Retrieved February 19, 2025, from https://www.gnu.org/software/diffutils/manual/html_node/patch-Options.html ↩ ↩2
-
Git. (n.d.). git-format-patch. Retrieved February 19, 2025, from https://git-scm.com/docs/git-format-patch ↩
-
Git. (n.d.). git-am. Retrieved February 19, 2025, from https://git-scm.com/docs/git-am ↩ ↩2