Skip to content

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.

Terminal window
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> ]
OptionDescription
-o , —output-directory Output to specified directory
—stdoutOutput to standard output
-s, —signoffAdd signoff line
-n, —numberedUse [PATCH n/m] subject prefix
-N, —no-numberedDon’t use patch numbers in subject
—start-number Start numbering patches at
—cover-letterGenerate cover letter
—no-cover-letterDon’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
-kDon’t strip/comment patches
—suffix=.Use different suffix (default .patch)
ParameterDescription
[]
Terminal window
git format-patch -3

Create patch files for the last 3 commits in current branch.

Terminal window
git format-patch HEAD~5

Format all commits from HEAD~5 to HEAD.

Terminal window
git format-patch -o ~/patches HEAD~10

Create patches in ~/patches directory for last 10 commits.

Terminal window
git format-patch --rfc HEAD~3

Create RFC-style patches with [RFC PATCH] subject prefix.

Terminal window
git format-patch --cover-letter HEAD~5

Create patches with a cover letter for the series.

Terminal window
git format-patch --range-diff=main HEAD~3

Include range diff in cover letter showing revisions.

Terminal window
git format-patch --stdout HEAD~1 > feature.patch

Output single patch to stdout (redirected to file).

Terminal window
git format-patch --to=johndoe@example.com --cc=team@example.com HEAD~5

Create patches with To: and Cc: headers for direct mailing.

Terminal window
git format-patch --start-number=100 HEAD~10

Start patch numbering from 100 instead of 1.

Terminal window
git format-patch . HEAD~20..HEAD

Format 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:

Terminal window
git format-patch <since>

This creates numbered patch files for each commit.

To include a cover letter with your patch series, use:

Terminal window
git format-patch --cover-letter HEAD~5

This creates 0000-cover-letter.patch with series overview.

To specify output directory, use -o:

Terminal window
git format-patch -o ~/patches HEAD~10

To create RFC-style patches, use —rfc:

Terminal window
git format-patch --rfc HEAD~3

This uses [RFC PATCH] instead of [PATCH] in subjects.

To preview what format-patch will create, use:

Terminal window
git format-patch --stdout HEAD~1 | head -50

To start numbering from a specific number, use:

Terminal window
git format-patch --start-number=10 HEAD~5

To include range diff showing revisions, use:

Terminal window
git format-patch --range-diff=main --cover-letter HEAD~3

How 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:

Terminal window
git format-patch --thread --to=maintainer@example.com HEAD~5

This adds proper threading headers for mailing list.

To test patches before distribution, use:

Terminal window
git am --dry-run < patch-file

This checks what git am would do without actually applying.

For large patch sets, consider using:

Terminal window
git format-patch --cover-letter --range-diff=v1.0 HEAD~50

This 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”
  1. Preparing patches for code review via email
  2. Contributing to projects that accept patch emails
  3. Creating backup of work for offline development
  4. Generating patch series for maintainer review
  5. Preparing patches for kernel and open source project submissions
  6. Creating reproducible patch archives for distribution