format-patch Git Command Guide
The git format-patch command is used to prepare each commit with its patch in a message format suitable for email submission. It formats commits as UNIX mailbox messages with metadata headers, commit messages, and unified diffs for use with git am.
git format-patch Syntax:
Section titled “git format-patch Syntax:”git format-patch [-k] [(-o|--output-directory) <dir> | --stdout] [--no-thread | --thread[=<style>]] [-s | --signoff] [-n | --numbered | -N | --no-numbered] [--start-number <n>] [--in-reply-to=<message-id>] [--subject-prefix=<prefix>] [--rfc[=<rfc>]] [--to=<email>] [--cc=<email>] [--[no-]cover-letter] [--range-diff=<previous>] [<common-diff-options>] [ <since> | <revision-range> ]Options:
Section titled “Options:”| Option | Description |
|---|---|
| -o | Output to specified directory |
| —stdout | Output to standard output |
| -s, —signoff | Add signoff line |
| -n, —numbered | Use [PATCH n/m] subject prefix |
| -N, —no-numbered | Don’t use patch numbers in subject |
| —start-number | Start numbering patches at |
| —cover-letter | Generate cover letter |
| —no-cover-letter | Don’t generate cover letter |
| —subject-prefix= | Custom subject prefix |
| —rfc[= | Use RFC prefix for subject |
| —range-diff= | Add range diff to cover letter |
| —to= | Add To: header |
| —cc= | Add Cc: header |
| —in-reply-to= | Set In-Reply-To header |
| —thread[= | Add threading headers |
| -k | Don’t strip/comment patches |
| —suffix=. | Use different suffix (default .patch) |
Parameters:
Section titled “Parameters:”| Parameter | Description |
|---|---|
| [ |
git format-patch Command Samples:
Section titled “git format-patch Command Samples:”Format last 3 commits
Section titled “Format last 3 commits”git format-patch -3Create patch files for the last 3 commits in current branch.
Format from specific commit
Section titled “Format from specific commit”git format-patch HEAD~5Format all commits from HEAD~5 to HEAD.
Output to specific directory
Section titled “Output to specific directory”git format-patch -o ~/patches HEAD~10Create patches in ~/patches directory for last 10 commits.
Format as RFC patch
Section titled “Format as RFC patch”git format-patch --rfc HEAD~3Create RFC-style patches with [RFC PATCH] subject prefix.
Include cover letter
Section titled “Include cover letter”git format-patch --cover-letter HEAD~5Create patches with a cover letter for the series.
Format with range diff
Section titled “Format with range diff”git format-patch --range-diff=main HEAD~3Include range diff in cover letter showing revisions.
Output to stdout
Section titled “Output to stdout”git format-patch --stdout HEAD~1 > feature.patchOutput single patch to stdout (redirected to file).
Add email headers
Section titled “Add email headers”git format-patch --to=johndoe@example.com --cc=team@example.com HEAD~5Create patches with To: and Cc: headers for direct mailing.
Custom numbering
Section titled “Custom numbering”git format-patch --start-number=100 HEAD~10Start patch numbering from 100 instead of 1.
Format to current directory
Section titled “Format to current directory”git format-patch . HEAD~20..HEADFormat patches in current directory for specified range.
How do I create patches for email submission?
Section titled “How do I create patches for email submission?”To create patches for email, run:
git format-patch <since>This creates numbered patch files for each commit.
How can I include a cover letter?
Section titled “How can I include a cover letter?”To include a cover letter with your patch series, use:
git format-patch --cover-letter HEAD~5This creates 0000-cover-letter.patch with series overview.
How do I change the output directory?
Section titled “How do I change the output directory?”To specify output directory, use -o:
git format-patch -o ~/patches HEAD~10How do I create RFC patches?
Section titled “How do I create RFC patches?”To create RFC-style patches, use —rfc:
git format-patch --rfc HEAD~3This uses [RFC PATCH] instead of [PATCH] in subjects.
How can I preview patch output?
Section titled “How can I preview patch output?”To preview what format-patch will create, use:
git format-patch --stdout HEAD~1 | head -50How do I control patch numbering?
Section titled “How do I control patch numbering?”To start numbering from a specific number, use:
git format-patch --start-number=10 HEAD~5How do I add range diff to cover letter?
Section titled “How do I add range diff to cover letter?”To include range diff showing revisions, use:
git format-patch --range-diff=main --cover-letter HEAD~3How do I format patches for mailing lists?
Section titled “How do I format patches for mailing lists?”To prepare patches for mailing list submission, use:
git format-patch --thread --to=maintainer@example.com HEAD~5This adds proper threading headers for mailing list.
How do I test patches before sending?
Section titled “How do I test patches before sending?”To test patches before distribution, use:
git am --dry-run < patch-fileThis checks what git am would do without actually applying.
How do I handle large patch sets?
Section titled “How do I handle large patch sets?”For large patch sets, consider using:
git format-patch --cover-letter --range-diff=v1.0 HEAD~50This creates well-documented series with overview and revision diffs.
Applications of the git format-patch command
Section titled “Applications of the git format-patch command”- Preparing patches for code review via email
- Contributing to projects that accept patch emails
- Creating backup of work for offline development
- Generating patch series for maintainer review
- Preparing patches for kernel and open source project submissions
- Creating reproducible patch archives for distribution