Skip to main content

Sign in

Save your progress and access it from any device.

Or with email

Don't have an account?

Privacy policy

Git apply: apply a patch to your code

git apply takes a patch file and applies the changes to your code. Ideal for sharing fixes without going through a branch or pull request.

Sharing

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.patch

Apply a patch

git apply --check file.patch

Verify without applying

git apply -R file.patch

Undo 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.

For: simple patches (git diff > file.patch)

git am

Applies an email-formatted patch and automatically creates a commit with the original author.

For: patches from git format-patch

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

A

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.

Start the investigations