git
format-patch
patch application
version control
software development

How to apply a patch generated with git format-patch?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

A patch created with git format-patch is usually meant to be applied with git am, not with a generic patch tool. That is because format-patch produces an email-style patch that includes commit metadata such as author, date, and commit message. If you apply it correctly, Git recreates the commit history rather than only editing files.

What git format-patch Produces

A patch generated by git format-patch contains:

  • the diff itself
  • the commit message
  • author information
  • date and metadata headers

That makes it different from a plain diff generated with git diff.

Typical generation commands look like this:

bash
git format-patch -1 HEAD
git format-patch origin/main..HEAD

These create one or more .patch files representing commits.

The Normal Way to Apply It: git am

If you received one patch file:

bash
git am 0001-some-change.patch

If you received a series:

bash
git am *.patch

This replays the commits into your current branch using the metadata from the patch files.

That is usually exactly what the sender intended when they used format-patch.

Check Your Working Tree First

Before applying patches, make sure your working tree is clean.

bash
git status

If you already have local edits, applying a patch series becomes harder to reason about and easier to break.

A good practice is to create or switch to a dedicated branch first:

bash
git switch -c review/patch-series

Now the incoming changes are isolated from unrelated work.

Handling Conflicts During git am

If a patch does not apply cleanly, git am stops and lets you resolve the conflict.

Useful commands are:

bash
1git status
2git am --continue
3git am --abort
4git am --skip

Workflow:

  1. inspect the conflict
  2. resolve the file manually
  3. stage the resolved files with git add
  4. run git am --continue

If the series should not be applied after all, use git am --abort to return to the pre-apply state.

When git apply Is the Wrong Tool

You can technically use git apply on patch content, but that only applies file changes. It does not recreate the original commit metadata the way git am does.

So use:

  • 'git am for format-patch output'
  • 'git apply for raw diffs when you only want the file changes'

This distinction matters in workflows where author attribution and commit boundaries should be preserved.

Preview and Validate Before Applying

You can inspect the patch content directly:

bash
sed -n '1,80p' 0001-some-change.patch

And you can ask Git whether the patch is likely to apply cleanly:

bash
git apply --check 0001-some-change.patch

Even when you plan to use git am, this check can be useful as a quick compatibility test.

Applying a Patch From Email or Standard Input

If the patch is piped into your shell or copied from an email mailbox format, git am can read from standard input too.

bash
git am < 0001-some-change.patch

This is especially common in email-driven workflows.

Common Pitfalls

  • Using git apply when the patch was clearly produced by git format-patch.
  • Applying patches on top of unrelated uncommitted work.
  • Forgetting that a patch series may contain multiple commits that should be applied in order.
  • Resolving a conflict but forgetting git add before git am --continue.
  • Applying the series to the wrong branch or wrong base commit.

Summary

  • Patches from git format-patch are normally applied with git am.
  • 'git am preserves author, message, and commit structure.'
  • Use a clean working tree and preferably a dedicated branch before applying.
  • Resolve conflicts with git am --continue or abandon the series with git am --abort.
  • Reserve git apply for raw diffs when you only want file changes, not recreated commits.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.