Patches: diff, format-patch, am, apply and send-email
Share changes as patch files: plain diffs and git apply, commit-preserving patches with format-patch and git am, cover letters and versioned series, handling patches that don't apply with --3way, emailing patches with send-email, and comparing revised series with range-diff.
kvi5hnuCode Sprout team member7 min readOn this page
Before pull requests, and still today in projects such as the Linux kernel and Git itself, changes travel as patches: text files describing changes, sent by email or attached to a ticket. Patches are also handy when there's no shared remote: sending a fix to someone outside your company, applying a change from a bug report, or moving work between machines that can't reach each other. Git has two kinds: plain diffs, applied with git apply, and mailbox patches that keep each commit's author and message, created with git format-patch and applied with git am.
Plain diffs and git apply
Any git diff output is a patch:
git init -q trail-guide && cd trail-guide
echo "Lake loop: 5 km" > lake.txt; git add .; git commit -q -m "Add lake page"
cd ..; git clone -q trail-guide copy; cd trail-guide
echo "Lake loop: 5.4 km" > lake.txt
git diff > ../distance.diff # the change, as a patch file
git restore lake.txt
cd ../copy
git apply --stat ../distance.diff # what it changes
git apply --check ../distance.diff && echo "applies cleanly"
git apply ../distance.diff
git status --short
lake.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
applies cleanly
M lake.txt
git apply <file>changes the working tree, and with--indexalso stages the changes. It doesn't commit, and the diff carries no author or message.--statsummarizes, and--checktests whether the patch would apply without changing anything.-Rapplies a patch in reverse, which undoes it.--3wayfalls back to a three-way merge when the context doesn't match, leaving conflict markers.--rejectapplies what it can and writes the rest to.rejfiles.- For binary files, create the diff with
git diff --binary.
Plain diff -u output from other tools works too, and git apply handles patches made outside Git, with -p<n> to strip leading path components.
format-patch and am: patches that keep commits
git format-patch turns commits into files, one per commit, in an email-like format that includes the author, date and message:
git init -q --bare server.git
git clone -q server.git sam 2>/dev/null; cd sam
echo "Lake loop: 5 km" > lake.txt; git add .; git commit -q -m "Add lake page"; git push -q origin main
cd ..; git clone -q server.git ana; cd ana
git config user.name "Ana Diaz"; git config user.email ana@example.com
echo "Lake loop: 5.4 km" > lake.txt; git commit -q -am "Fix lake distance"
echo "Ridge loop: 8 km" > ridge.txt; git add .; git commit -q -m "Add ridge page"
git format-patch origin/main -o ../patches
echo "=== the start of the first patch"
head -5 ../patches/0001-Fix-lake-distance.patch
cd ../sam
git am ../patches/*.patch
git log --format='%h author: %an, committer: %cn | %s'
../patches/0001-Fix-lake-distance.patch
../patches/0002-Add-ridge-page.patch
=== the start of the first patch
From 839e8cb63a3cec40604670c7c74c49a326fd120b Mon Sep 17 00:00:00 2001
From: Ana Diaz <ana@example.com>
Date: Mon, 2 Mar 2026 10:00:00 +0000
Subject: [PATCH 1/2] Fix lake distance
Applying: Fix lake distance
Applying: Add ridge page
8174808 author: Ana Diaz, committer: Sam Lee | Add ridge page
785ee05 author: Ana Diaz, committer: Sam Lee | Fix lake distance
65a5265 author: Sam Lee, committer: Sam Lee | Add lake page
git format-patch <since>creates a patch for each commit after<since>, here every commit not yet onorigin/main.-3takes the last three commits, and-1 <commit>one specific commit.- Each file starts with a
From <commit-id>line and mail headers:From:(the author),Date:andSubject: [PATCH 1/2] .... Then come the rest of the message, a diffstat and the diff. git am("apply mailbox") applies each patch as a commit, keeping the author, date and message. The person applying becomes the committer, which is why author and committer differ in the log.-o <dir>chooses the output folder, and--stdoutwrites everything to standard output, as ingit format-patch -1 --stdout > fix.patch.
Series, versions and cover letters
git format-patch --cover-letter -3 -o outgoing/ # 0000-cover-letter.patch to describe the series
git format-patch -v2 --cover-letter -3 -o outgoing/ # [PATCH v2 1/3] after review changes
git format-patch --base=origin/main -3 # record which commit the series applies to
git format-patch --range-diff=v1-branch -v2 -3 # include what changed since version 1
A cover letter (0000-cover-letter.patch) is a template to fill in with a summary of the whole series. Resend a revised series with -v2, -v3 and so on, so reviewers can tell versions apart.
When a patch doesn't apply
If the files changed since the patch was made, git am stops at the failing patch:
git init -q trail-guide && cd trail-guide
printf 'Lake loop: 5 km\nRidge loop: 8 km\nCave loop: 3 km\nRiver loop: 7 km\nMeadow loop: 4 km\nForest loop: 6 km\n' > trails.txt
git add .; git commit -q -m "Add trails"
git switch -q -c ana
sed -i 's/Forest loop: 6 km/Forest loop: 6.5 km/' trails.txt; git commit -q -am "Fix forest distance"
git format-patch -1 -o ../patches > /dev/null
git switch -q main
sed -i 's/Cave loop: 3 km/Cave loop: 3 km (bring a torch)/' trails.txt
git commit -q -am "Describe cave loop"
git am ../patches/*.patch > /dev/null 2>&1; echo "plain am: exit $?"
git am --abort
git am --3way ../patches/*.patch
git log --oneline -3; cat trails.txt
plain am: exit 128
Applying: Fix forest distance
Using index info to reconstruct a base tree...
M trails.txt
Falling back to patching base and 3-way merge...
Auto-merging trails.txt
5b4f9fc Fix forest distance
fafe3df Describe cave loop
06308d9 Add trails
Lake loop: 5 km
Ridge loop: 8 km
Cave loop: 3 km (bring a torch)
River loop: 7 km
Meadow loop: 4 km
Forest loop: 6.5 km
Plain git am requires the patch's context lines to match, and they no longer do. --3way uses the blob IDs recorded in the patch's index lines to find the original files, if your repository has them, and performs a real three-way merge. That usually succeeds, as here, or stops with normal conflict markers. Set am.threeWay=true to make it the default.
When git am stops:
| Command | Effect |
|---|---|
fix the files, git add, then git am --continue | finish this patch, continue with the rest |
git am --skip | skip this patch |
git am --abort | undo the whole am session |
git am --show-current-patch=diff | see the patch that failed |
Emailing patches: send-email
Mailing-list projects expect patches sent as plain-text emails, one per commit, which git send-email does:
git config --global sendemail.smtpServer smtp.example.com
git config --global sendemail.smtpUser sam@example.com
git config --global sendemail.smtpServerPort 587
git config --global sendemail.smtpEncryption tls
git send-email --to=dev@lists.example.org outgoing/*.patch
- Pasting patches into a normal email client usually breaks them, because clients rewrap lines and change whitespace. Use
send-email, or a client configured for plain text. --annotatelets you edit each email before sending, and--dry-runshows what would be sent.send-emailneeds Perl and some modules. A few minimal Git builds leave it out, which shows as "git was built without support for git-send-email".- Tools such as b4 help work with patch-based projects: fetching a series from a mailing list archive, and preparing and sending your own.
To apply patches received by email, save them as .mbox or .eml files, and run git am <file>.
Comparing versions: range-diff
When a series is revised, reviewers want to know what changed between v1 and v2, not to read everything again. git range-diff compares two versions of a series commit by commit:
git range-diff main v1-branch v2-branch # or: git range-diff v1-base..v1-tip v2-base..v2-tip
It pairs up corresponding commits, and shows = for unchanged ones and a diff of the diffs for changed ones. That's useful for pull requests too, after a force push.
Patches or pull requests?
Pull requests (article 22) are the norm on hosting services: discussion, review comments and CI are built in. Patches remain the workflow for Git, Linux and other mailing-list projects, and are useful whenever two repositories can't reach the same remote. Bundles (article 46) are the alternative for moving many commits, including branches and history, as one file.
What's current
- Pull requests dominate on hosting services, but
format-patch,amandsend-emailare the standard workflow for the Linux kernel, Git and many systems projects. - b4 has become the common helper for kernel-style patch workflows.
git range-diffis widely used to review revised series and force-pushed pull requests.
Common questions
What's the difference between git apply and git am?
git apply applies a diff to your files, without committing. git am applies format-patch output as commits, keeping each commit's author, date and message.
git am failed halfway. How do I get back to where I was?
git am --abort undoes the whole session. Or fix the conflict, git add, and git am --continue.
Can I make a patch of uncommitted changes?
Yes: git diff > change.diff for unstaged, git diff --staged for staged, and git diff HEAD for both. Apply with git apply. For a commit with author and message, commit first and use format-patch.
Why does a patch fail with "patch does not apply"?
The lines around the change differ in your copy. Try git am --3way or git apply --3way, which merge using the original file versions when your repository has them, or apply to the commit the patch was made from (--base records it).
Summary
git diff > file.diffandgit applymove uncommitted changes.--check,--stat,-Rand--3wayhelp.git format-patch <since>makes one email-style file per commit, andgit amapplies them as commits with the original author and message.--cover-letter,-v2and--range-diffsupport reviewed patch series.git am --3wayresolves most patches that don't apply directly.--continue,--skipand--abortcontrol a stopped session.git send-emailsends patches as plain-text emails.git range-diffcompares versions of a series.
Practice questions
A contributor without push access wants to send you their last three commits. What should they run, and what do you run?
Answer
They run git format-patch -3 -o patches/ (or git format-patch origin/main) and send the files. You run git am patches/*.patch, which creates three commits with their authorship and messages. Use git am --3way if your branch has moved on.
Why is the committer different from the author after git am?
Answer
The author is whoever wrote the change, taken from the patch's From: header. The committer is whoever applied it to this repository, which is you. Git records both, and git log --format='%an %cn' shows them.
git am stops with "patch does not apply". List two ways forward.
Answer
Abort with git am --abort, then retry with git am --3way, which merges using the original blobs if you have them. Or, inside the stopped session, apply the patch manually, for example with git apply --reject and fixing the .rej hunks by hand, then git add and git am --continue. git am --skip skips that patch entirely.
Previous: Large Repositories: Sparse Checkout, Partial Clone, Maintenance and Scalar

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.