Git Patches: Sharing Commits as Files
A patch is a text file that describes changes to files: which lines were removed and which were added, in the diff format described in "How to Read a Diff". Git can create patches from your work and apply patches from others. A plain patch (from git diff) carries only the changes. A patch made with git format-patch carries a whole commit: the author, date, message and changes, so that git am can recreate the commit in another repository. Patches are used to share work without a shared server, such as by email or a USB drive, and are how the Linux kernel and Git itself accept contributions.
It is like sending a recipe change as a note: "in step 3, replace two cups of sugar with one". The other cook doesn't need your kitchen, only their copy of the recipe and your note.
Syntax
git diff > <file>.patch
git apply <file>.patch
git format-patch <range> [-o <folder>]
git am <file>.patch...| Command | Description |
|---|---|
git diff > fix.patch | It is used to save uncommitted changes as a plain patch. |
git apply --check <patch> | It is used to test whether a patch would apply, without changing anything. |
git apply <patch> | It is used to apply a plain patch to the working files (no commit is made). |
git format-patch -<n> | It is used to write the last n commits as numbered patch files, one per commit. |
git format-patch <since>..<until> | It is used to write the commits in a range as patch files. |
git am <patch>... | It is used to apply patch files made by format-patch as commits, keeping their author and message. |
git am --abort / --continue / --skip | It is used to cancel, resume after fixing, or skip a patch that failed. |
git am -3 | It is used to fall back to a three-way merge when a patch doesn't apply cleanly. |
The Starting Point
Navika and Advitha each have a copy of the Fee Tracker, but no shared server. Advitha's copy was cloned from Navika's folder, so both start at the same commit:
mkdir fee-tracker
cd fee-tracker
git init --quiet
printf "%s\n" 'Term 1 fee: $12,000' 'Late fee: $250' > fees.txt
git add fees.txt
git commit --quiet -m "Add the fee list"
cd ..
git clone --quiet fee-tracker advitha
git -C advitha config user.name "Advitha Anamaneni"
git -C advitha config user.email advitha@example.com Example 1: A Plain Patch from git diff
Navika has an uncommitted change that she wants Advitha to try:
cd fee-tracker
sed -i.bak 's/$250/$300/' fees.txt && rm fees.txt.bak
git diff > ../late-fee.patch
git restore fees.txt
cat ../late-fee.patchdiff --git a/fees.txt b/fees.txt
index b73e396..872c153 100644
--- a/fees.txt
+++ b/fees.txt
@@ -1,2 +1,2 @@
Term 1 fee: $12,000
-Late fee: $250
+Late fee: $300Explanation: the patch is exactly what git diff printed: one line removed, one added, with a line of context. It has no author, date or message, because uncommitted changes don't have them. Navika restored her own file, as she only wanted to share the idea.
Advitha checks and applies it:
cd ../advitha
git apply --check ../late-fee.patch && echo "The patch applies cleanly"
git apply ../late-fee.patch
git status --short
git restore fees.txtThe patch applies cleanly
M fees.txtExplanation: git apply --check printed nothing (no problems), so && ran the echo. git apply changed the working file, but made no commit: the change is now Advitha's uncommitted work. She restored it after trying it out.
Example 2: Commits as Patch Files
Navika makes three commits and sends them as patches:
cd ../fee-tracker
echo 'Hostel fee: $9,000' >> fees.txt
git commit --quiet -am "Add the hostel fee"
echo 'Exam fee: $800' >> fees.txt
git commit --quiet -am "Add the exam fee"
echo 'Bus fee: $1,500' >> fees.txt
git commit --quiet -am "Add the bus fee"
git format-patch -3 -o ../patches../patches/0001-Add-the-hostel-fee.patch
../patches/0002-Add-the-exam-fee.patch
../patches/0003-Add-the-bus-fee.patchExplanation: git format-patch -3 wrote the last three commits as three files, numbered in order and named after each commit's subject. -o ../patches put them in a folder. A range works too: git format-patch main..feature/fees writes every commit on the feature branch.
Each file looks like an email:
cat ../patches/0001-Add-the-hostel-fee.patchFrom cd4b0f0b701f68962da585b0bf9cf5233a82b772 Mon Sep 17 00:00:00 2001
From: Navika Anamaneni <navika@example.com>
Date: Mon, 5 Jan 2026 10:28:00 -0600
Subject: [PATCH 1/3] Add the hostel fee
---
fees.txt | 1 +
1 file changed, 1 insertion(+)
diff --git a/fees.txt b/fees.txt
index b73e396..e6cd827 100644
--- a/fees.txt
+++ b/fees.txt
@@ -1,2 +1,3 @@
Term 1 fee: $12,000
Late fee: $250
+Hostel fee: $9,000
--
2.53.0Explanation: the first line, From <commit ID> Mon Sep 17 00:00:00 2001, is a fixed marker that makes the file a valid mailbox; the date in it is always the same and means nothing. Then come the real author, the author date and the subject [PATCH 1/3]. After --- come a summary of changed files and the diff, and the file ends with the Git version that made it. Everything between the subject and --- is the commit message body.
Example 3: Applying Patches with git am
cd ../advitha
git am ../patches/*.patch
git log --format="%h %an (committed by %cn): %s"Applying: Add the hostel fee
Applying: Add the exam fee
Applying: Add the bus fee
3c28d32 Navika Anamaneni (committed by Advitha Anamaneni): Add the bus fee
f232088 Navika Anamaneni (committed by Advitha Anamaneni): Add the exam fee
290daa7 Navika Anamaneni (committed by Advitha Anamaneni): Add the hostel fee
2ac92ac Navika Anamaneni (committed by Navika Anamaneni): Add the fee listExplanation: git am (apply mailbox) turned each file back into a commit, in order. The author is still Navika, with her original date, while Advitha is the committer: exactly how Git records "written by one person, applied by another". The commit IDs differ from Navika's, because the committer and commit time are different.
Example 4: A Patch That Doesn't Apply
Advitha has meanwhile changed the bus fee herself, and Navika sends a patch that changes the same line:
sed -i.bak 's/Bus fee: $1,500/Bus fee: $1,600/' fees.txt && rm fees.txt.bak
git commit --quiet -am "Raise the bus fee"
cd ../fee-tracker
sed -i.bak 's/Bus fee: $1,500/Bus fee: $1,400/' fees.txt && rm fees.txt.bak
git commit --quiet -am "Lower the bus fee"
git format-patch --quiet -1 -o ../patches
cd ../advitha
git apply --check ../patches/0001-Lower-the-bus-fee.patcherror: patch failed: fees.txt:2
error: fees.txt: patch does not applyExplanation: the patch expects the line Bus fee: $1,500 (the 2 in fees.txt:2 is where its lines start), but Advitha's file has $1,600, so it can't apply. git apply --check works on format-patch files too, and is a safe way to test a patch before git am.
If she runs git am anyway, it stops at this patch. This is what a terminal shows. It mixes the two output streams line by line, which this page's checker can't reproduce, so the output below was copied from a real terminal:
git am ../patches/0001-Lower-the-bus-fee.patchApplying: Lower the bus fee
error: patch failed: fees.txt:2
error: fees.txt: patch does not apply
Patch failed at 0001 Lower the bus fee
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"Explanation: git am stopped in the middle of the series and told her the three ways out. Here it is again, with its messages hidden by > /dev/null 2>&1, so that the state it leaves behind can be checked:
git am ../patches/0001-Lower-the-bus-fee.patch > /dev/null 2>&1
git status | grep "am session"
git am --abort
git log --oneline -1You are in the middle of an am session.
588b502 (HEAD -> main) Raise the bus feegit status reminds her that an am session is in progress. She decides her own change stands, and git am --abort puts the branch back as it was.
To resolve the conflict instead, she would run git am -3 <patch>. With -3, Git uses the blob IDs on the patch's index line (if her repository has the original file version) to do a three-way merge, leaving normal conflict markers to fix as in "Resolving Merge Conflicts", followed by git add and git am --continue.
The Mailing-List Workflow
Projects such as the Linux kernel and Git itself take contributions by email, not pull requests. git send-email sends patch files as emails, and git request-pull writes a summary asking a maintainer to pull a branch. Both need a mail server or a published repository, so they are shown without output:
git format-patch --cover-letter -3 -o outgoing/
git send-email --to=maintainer@example.com outgoing/*.patch
git request-pull v1.0 https://github.com/navika-anamaneni/fee-tracker.git mainExplanation: --cover-letter adds a 0000-cover-letter.patch file, an introductory email for the series that you edit before sending. git send-email needs SMTP settings (sendemail.smtpServer and others) and is installed separately on some systems. Reviewers reply to each email with comments, and the author sends a new version with git format-patch -v2, which marks the subjects [PATCH v2 1/3].
Patches vs Other Ways to Share Work
| Way | Needs a shared server | Keeps author and message | Good for |
|---|---|---|---|
| Push and pull request | Yes | Yes | Everyday team work |
git format-patch + git am | No | Yes | Email workflows, small contributions, air-gapped networks |
git diff + git apply | No | No | Quick "try this change" |
git bundle (see "Git Bundle and Archive") | No | Yes, with full history | Moving many commits or a whole repository |
Common Mistakes
Using git apply on a format-patch file. It changes the files but loses the author and message. Use git am.
Using git am on a plain git diff patch. It fails, because there is no author or subject. Use git apply.
Forgetting --abort after a failed git am. The repository stays in the middle of an am session; git status says so. Finish with --continue, --skip or --abort.
Applying patches out of order. Patch 2 may depend on patch 1. Apply them in their numbered order: git am ../patches/*.patch does this, because the shell sorts the names.
Editing patch files in an editor that changes whitespace. Trailing spaces and tabs matter in a diff.
Under the Hood
git format-patch writes each commit in the mbox email format: the fixed From <id> Mon Sep 17 00:00:00 2001 separator line, email headers (From, Date, Subject), the message body, the --- line, a diffstat and the diff. The diff includes index <old>..<new> lines with the blob IDs before and after. git am splits a mailbox into messages (git mailsplit), reads the headers and body of each (git mailinfo), applies the diff with git apply, and commits with the author and date from the headers. With -3, if git apply fails, Git looks up the index blob IDs in your repository and, if it has them, builds a small temporary tree to run a three-way merge, which is why -3 only helps when you share history with the sender.
Interview Corner
What is the difference between git apply and git am?
git apply applies a diff to the working files without committing. git am applies patch files made by git format-patch as commits, keeping the original author, date and message.
How do you send your last three commits to someone without a shared server?
git format-patch -3 writes three patch files. The other person runs git am *.patch to recreate the commits.
A git am fails halfway through. What are your options?
Fix the problem and run git am --continue, skip that patch with git am --skip, or cancel the whole series with git am --abort. git am -3 retries with a three-way merge.
Who is the author of a commit applied with git am?
The original author from the patch. The person who ran git am becomes the committer.
Practice
Challenge: one patch for a branch. In Navika's repository, create a branch fix/exam-fee from main, commit a change to the exam fee, and write the branch's commits as patches into ../outgoing. Then show the subject line of the patch.
What does Git print? Before you open the solution, decide the file name and the subject line.
Show solution
cd ../fee-tracker
git switch --quiet -c fix/exam-fee
sed -i.bak 's/Exam fee: $800/Exam fee: $850/' fees.txt && rm fees.txt.bak
git commit --quiet -am "Raise the exam fee"
git format-patch main..fix/exam-fee -o ../outgoing
grep "^Subject" ../outgoing/*.patch../outgoing/0001-Raise-the-exam-fee.patch
Subject: [PATCH] Raise the exam feeThe range main..fix/exam-fee contains one commit, so the file is 0001-Raise-the-exam-fee.patch. With only one patch, the subject is [PATCH], without numbers (add --numbered to force [PATCH 1/1]).
Checkpoint. Make two clones of a practice repository. In one, make three commits and send them to the other as patch files with git format-patch and git am. Check with git log --format="%an %cn %s" who is author and committer.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git diff > fix.patch | Save uncommitted changes |
git apply --check fix.patch | Test a patch |
git apply fix.patch | Apply a plain patch |
git format-patch -<n> -o <folder> | Write commits as patch files |
git am <files> | Apply patch files as commits |
git am --abort | Cancel a failed am |
Was this tutorial helpful?