How to Apply a Patch in Git
git apply takes a patch file and applies the changes it describes to your working directory. It doesn't create a commit or touch your branch history — it just modifies the files. You then stage and commit as you normally would.
A patch file is essentially the output of git diff: a description of which lines were added or removed. This makes it a practical way to share changes without sharing repository access — a contributor who doesn't have push access can send you a .patch file by email, and you can apply it locally to test before deciding what to do with it. This email-based patch workflow is still common in many open-source projects.
git apply bugfix.patch
Checking before You Apply
Before applying a patch, it's worth checking whether it'll apply cleanly. Use --check to do a dry run without touching any files:
git apply --check feature.patch
If there's no output, the patch applies cleanly. If something's wrong, you'll see exactly where it fails:
error: patch failed: src/main.js:100
error: src/main.js: patch does not apply
Applying and Staging at Once
By default, git apply modifies your working directory but leaves the changes unstaged. If you want to apply the patch and stage everything in one step, use --index:
git apply --index bugfix.patch
After this, git status will show the changes as staged and ready to commit, rather than just modified.
If you want to stage the changes without touching the working directory at all, use --cached instead.
Applying a Fix to a Different Branch
You have a bug fix on one branch and want it on another, but you don't want to merge the whole branch. Export just that commit as a patch and apply it:
git format-patch -1 <commit_hash>
git apply 0001-your-fix.patch
This also works across unrelated repositories — if two projects share similar code and you want to port a change from one to the other, a patch file is a straightforward way to do it.
When to Use git am Instead
git am (short for "apply mailbox") is the right tool when you're applying patches that were generated with git format-patch — for example, a series of commits sent over email. Unlike git apply, it creates commits automatically and preserves the original author information, date, and commit message.
This workflow is common in projects like the Linux kernel, where contributors send patch series by email. If you receive patches in mbox format, git am can process them directly.
Use git apply when you just want the file changes and will handle the commit yourself. Use git am when you want to replay a series of commits as-is, including their metadata. It also lets you cherry-pick specific patches from a series, rather than applying all of them.
Creating Patch Files
From uncommitted changes:
git diff > changes.patch
From a single commit (produces a .patch file named after the commit):
git format-patch -1 <commit_hash>
From the difference between two branches:
git diff branch1..branch2 > branch_diff.patch
You can learn more about git format-patch here.
What's in a Patch File
A patch file contains:
diff --gitlines marking the start of changes for each file@@hunk headers showing which line numbers are affected- Lines prefixed with
+for additions and-for removals
Get our popular Git Cheat Sheet for free!
You'll find the most important commands on the front and helpful best practice tips on the back. Over 100,000 developers have downloaded it to make Git a little bit easier.
About Us
As the makers of Tower, the best Git client for Mac and Windows, we help over 100,000 users in companies like Apple, Google, Amazon, Twitter, and Ebay get the most out of Git.
Just like with Tower, our mission with this platform is to help people become better professionals.
That's why we provide our guides, videos, and cheat sheets (about version control with Git and lots of other topics) for free.