Git Reference ยท Remotes and Collaboration
git format-patch command
Reference for git format-patch: turn commits into numbered mbox patch files, add cover letters and version numbers, and control subjects, output and recipients.
git format-patch turns commits into patch files in email format, one file per commit. Each file starts with mail headers (From, Date, Subject), then the commit message, a --- line, a diffstat and the diff itself. The result can be reviewed by email, attached to a ticket, or applied elsewhere with git am.
Unlike a plain diff, a format-patch file carries the author, the date and the message, so applying it recreates the commit rather than just changing files.
git format-patch [options] <since> | <revision-range> | -<n>
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
git format-patch [-k] [-o <dir> | --stdout] [--cover-letter] [-v <n>] [--subject-prefix=<text>] [--to=<addr>] [--cc=<addr>] [--from[=<ident>]] [-<n>] [--[no-]numbered] [--signature=<text>] [--base=<commit>] [<since> | <revision-range>]
| Option | Description |
|---|---|
<since> |
Format every commit reachable from HEAD but not from <since>, oldest first. A single revision means "since this one". |
-<n> |
Format the last n commits. -1 <commit> formats exactly that commit. |
-o <dir> |
Write files into <dir> (created if needed) instead of the current directory. |
--stdout |
Print all patches to standard output as a single mbox stream. |
--cover-letter |
Also generate 0000-cover-letter.patch with a subject and body to fill in, plus an overall diffstat. |
-v <n>, --reroll-count=<n> |
Mark the series as version n: [PATCH v2 1/3] and file names v2-0001-.... |
--subject-prefix=<text> |
Replace PATCH in the subject, for example RFC PATCH. --rfc is a shortcut for RFC. |
--to, --cc |
Add To: or Cc: headers for use with git send-email. |
-n, --numbered / -N, --no-numbered |
Control [PATCH n/m] numbering. Numbering is automatic for more than one patch. |
--numbered-files |
Name files 1, 2, ... without subject text. |
-k, --keep-subject |
Do not add [PATCH] or strip existing bracketed prefixes. |
--base=<commit> |
Add a base-commit: trailer and prerequisite patch IDs so the receiver knows the exact starting point. |
--signature=<text> / --no-signature |
Replace or drop the trailer line after the diff (default is the Git version). |
--from[=<ident>] |
Put an in-body From: line so the real author is kept when the sender differs. |
--attach, --inline |
Send the patch as an attachment or inline MIME part. |
-p, --no-stat |
Produce the plain patch without the diffstat. |
Returns: the file names written, one per line, on standard output (unless --stdout).
Edge cases: merge commits are skipped by default; use --no-merges explicitly if you want clarity, or rebase first. An empty range prints nothing and exits successfully.
Usage notes
- Subject-based names. File names are
NNNN-<sanitized-subject>.patch, limited to 64 characters by default (--filename-max-length). Use--numbered-filesfor stable names. - A single revision is not a single commit.
git format-patch HEAD~1means "everything after HEAD~1", which happens to be one commit.git format-patch -1 abc123is the way to export a specific commit. - Reviewing a revision of a series. Use
-v2,-v3andgit range-diffto show reviewers what changed between versions. - Dates and authors are preserved. Committer information is not; git am uses the sender's identity as committer.
- Binary files need
--binaryto be included andgit amneeds--3wayor full index lines to apply them. - Format configuration:
format.numbered,format.subjectPrefix,format.to,format.cc,format.signatureandformat.coverLetterset defaults. - Email layout of the produced files is described in Email patch format.