By admin , 29 April 2026

Synopsis

git apply [--check] [--index] [--3way] <patch>...

Description

The git apply command applies a unified diff (a patch file) to the working tree, the index, or both. Unlike git am, which expects a full mailbox-format patch with author and message metadata, git apply just applies the diff content. It's useful for applying patches received over email, code review tools, or generated by git diff.

By default, git apply only updates the working tree. Use --index to also update the staging area, and --3way to fall back to a three-way merge when the patch doesn't apply cleanly. To verify a patch without applying, use --check.

In day-to-day use, git apply integrates closely with shell aliases, editor plugins, and continuous integration. Power users often add aliases that combine flags they always pass, or wrap the command in scripts that enforce team conventions. Output formatting can be customized via Git config — pretty formats, color schemes, and pager behavior are all tunable. When something goes wrong, the first diagnostic step is usually to re-run the command with GIT_TRACE=1 in the environment, which reveals the underlying plumbing calls. For unusual situations, the --help output (git apply --help) opens the full manual page with details on every option, including those rarely used in casual workflows but essential for debugging or scripting at scale.

Understanding how git apply interacts with the rest of Git's data model — the object database, the index, refs, and the working tree — pays dividends. Each command operates on some subset of these pieces, and knowing which it touches helps predict outcomes and recover from mistakes. Reading the official Git documentation alongside hands-on practice in a throwaway repository is the fastest way to internalize the nuances. Most production issues with Git stem from one of three causes: surprising default behavior, partial network operations, or rewriting history that was already shared. A working mental model of git apply's side effects helps avoid all three.

Common Options

OptionDescription
--checkTest if the patch applies without modifying anything.
--indexApply to working tree and index.
--cachedApply to index only.
--3wayThree-way merge if context doesn't match.
--reverse / -RApply in reverse (uncommit a patch).
--whitespace=<action>Handle whitespace errors (warn, fix, error).
-p<n>Strip n path components from filenames.

Examples

git apply changes.patch
# Apply a patch file to the working tree

git apply --check feature.patch
# Verify a patch will apply cleanly

git apply --index --3way mr.diff
# Apply with three-way merge fallback, into index too

curl -L https://example.com/fix.patch | git apply
# Pipe a patch directly from a URL

Common Mistakes

If the patch was generated against a different base, plain git apply fails with "patch does not apply." Adding --3way often saves the day by attempting a merge. For patches that include commit metadata (author, message), use git am instead so the commit is recreated faithfully.

Related Commands

git am, git format-patch, git diff, git cherry-pick