Progress:
TIER 3 · MODULE 01· Expert

git apply

Lower-level than git am. No commit, no email parsing — just patch.

🎯 What & why

git apply consumes a unified diff and rewrites the working tree (and optionally the index) to match. It is the patch-engine half of git am — no mailbox parsing, no commit creation, just bytes-on-disk surgery.

🧠 Mental model

Pure working-tree (and optionally index) mutation. With --cached it touches only the index (staged blobs in the object DB + index entries); without it, files on disk change and the index is left alone unless --index is given. No refs move, no commit objects are created.

🛠️ Synopsis

git apply [--stat] [--numstat] [--summary] [--check] [--index | --cached]
          [-3 | --3way] [--whitespace=<action>] [-R | --reverse]
          [--reject] [-p<n>] [-C<n>] [--unidiff-zero] [--allow-empty]
          [--exclude=<pattern>] [--include=<pattern>] [<patch>...]

🎚️ Switches & options

FlagWhat it does
--checkDry-run: verify the patch would apply cleanly. Exits non-zero on failure.
--stat / --summaryShow a diffstat or rename/copy/mode summary instead of applying.
--indexApply to BOTH the working tree and the index (the patch must apply to the current index).
--cachedApply to the index ONLY; the working tree is untouched. Pairs well with bare or scripted flows.
-3, --3wayFall back to a 3-way merge using blob OIDs in the patch when context fails. Requires those blobs in the object DB.
-p<n>Strip <n> leading path components (default 1, like patch -p1). Use -p0 for paths with no a/ b/ prefix.
-C<n>Require <n> lines of matching context (default 3). Lower it to force-fit fuzzy patches.
--whitespace=<action>How to handle whitespace errors: nowarn|warn|fix|error|error-all. fix rewrites trailing-WS lines.
--reject⚠️ On failure, write .rej files for hunks that didn't apply. Leaves a partial mess — review every reject.
-R, --reverseApply the patch in reverse. Handy for unrolling a previously-applied diff.

💡 Use cases

🧪 Examples

Validate before applying
git apply --check 0001-fix.patch
Apply to index and worktree
git apply --index 0001-fix.patch
3-way fallback when context drifts
git apply -3 0001-fix.patch
Reverse-apply a hunk file
git apply -R bad-change.diff

🎓 Recommendations

🪤 Common pitfalls

🔗 Related modules

📝 Quiz

Hit each option, then Check answers. Score is recorded; Next is always open.