Command Reference

git-format-patch Tutorial

Explains how to use git-format-patch to export commits as patch files.

Who This Is For
  • Developers who already know basic commit and branch actions
  • Readers who want to understand command boundaries and risk
Prerequisites
  • A basic mental model of worktree, index, and commits
  • Comfort reading `git status` and a small commit graph
Common Risks
  • Using local cleanup commands on already shared history
  • Continuing to rewrite before confirming a recovery path

The short version

git-format-patch is used to export commits as patch files.

When it is a good fit

  • when you need to export commits as patch files
  • when you want this step to be repeatable instead of ad hoc
  • when you need a clearer mental model of what Git is recording or updating

Basic example

git format-patch origin/main..HEAD

What to watch most closely

Patch-based workflows are sensitive to context drift, so confirm the base state and the recovery path first.

A safer working habit

Read the patch series first and keep abort, continue, or a backup branch ready.

Useful angles for understanding it

  • Handle mailbox patches or patch files
  • Exchange changes outside direct repository access
  • Preserve commit boundaries instead of copying edits manually

Related reading

Read it alongside git status, git log, and git show so it is easier to see how the command changes history, refs, the index, or the working tree.

What problem this command solves in a workflow

git format-patch exports commits as patch files in email-ready format. Each commit becomes a separate .patch file containing the diff, commit message, author information, and email headers — making it the tool for "how do I package commits for review outside the repository?"

Typical use cases

  • Export a range of commits as individual patch files for code review: git format-patch origin/main..HEAD.
  • Send patches to a mailing list or contributors who do not have direct repository access.
  • Use --cover-letter to generate a summary email that describes the entire patch series at a high level.

Diagram view

Commit-to-patch exportformat-patch reads commits from the history and generates individual .patch files with diffs, metadata, and email headers.
Input
Commit range (e.g., origin/main..HEAD)Commit metadata
Output
Individual .patch files per commitOptional cover letter
Each patch file is self-contained and can be applied by git am or reviewed independently.

Special cases and boundaries

  • Clean up commit granularity and commit messages before generating patches, or downstream review and application will be noisier.
  • format-patch generates one file per commit; use -o <dir> to place all output files in a specific directory.
  • The --cover-letter option creates a template summary email that you should edit before sending, describing the overall purpose of the patch series.
  • Patches generated by format-patch are designed to be consumed by git am, preserving author identity and commit messages.