Git apply: apply a patch to your code
git apply file.patch applies the changes from a patch (a diff file) to your working directory, without creating a commit. Check it first with git apply --check. Ideal for sharing fixes without a branch or pull request.
By Anaïs Sparesotto, web developer and head of education · Updated
What is git apply?
git apply reads a patch file (a diff in text format) and applies the changes to your code. It is like copy-pasting a diff automatically.
Unlike git am, apply does not create a commit. The changes are applied to the working directory and you decide when to commit.
Git apply syntax
git apply file.patchApply a patch
git apply --check file.patchVerify without applying
git apply -R file.patchUndo a patch (reverse)
Git apply in practice
Applying and verifying patches.
Applying a patch
Verify and undo
git apply vs git am
git apply
Applies a raw diff to the working directory. No automatic commit. You stay in control.
git am
Applies an email-formatted patch and automatically creates a commit with the original author.
Common mistakes with git apply
The patch does not apply
The code has changed since the patch was created. Try git apply --3way for automatic resolution, or apply the changes manually.
Applying a format-patch with apply
Patches from format-patch contain email metadata. Use git am to apply them correctly with author and commit message.
Forgetting to verify with --check
Always run git apply --check first to verify that the patch applies cleanly before modifying your files.
Part of the Git Advanced guide
GitQuest is created by Anaïs (nouvelle fenêtre), web developer and head of education, specializing in tech training and digital accessibility.
Questions about git apply
Master patches with GitQuest
Learn to share fixes and collaborate using patches.