Skip to content
Latchkey

git apply: Usage, Options & Common CI Errors

git apply takes a diff/patch file and applies its changes - without creating commits.

apply is how you replay a diff in CI (e.g. a generated patch). Use --check first and --3way for resilience.

What it does

git apply reads a unified diff and modifies files in the working tree (and, with --index, the index) to match it. Unlike git am it does not create commits.

Common usage

Terminal
git apply changes.patch
git apply --check changes.patch    # dry run, validate only
git apply --3way changes.patch     # fall back to 3-way merge
git apply --index changes.patch    # also stage the result
git diff > out.patch && git apply out.patch

Options

FlagWhat it does
--checkValidate without applying
--3way / -3Use 3-way merge on conflict
--indexApply to the index as well
-p<n>Strip n leading path components
-R / --reverseApply the patch in reverse

Common errors in CI

error: patch failed: <file>:<n> / "error: <file>: patch does not apply" - the target file drifted from what the patch expects. Try git apply --3way, fix the path level with -p, or regenerate the patch against the current base.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →