LinuxCommandLibrary

git-format-patch

Create patch files from Git commits

TLDR

Create an auto-named .patch file for all the unpushed commits

$ git format-patch [origin]
copy

Write a .patch file for all the commits between 2 revisions to stdout
$ git format-patch [revision_1]..[revision_2]
copy

Write a .patch file for the 3 latest commits
$ git format-patch -[3]
copy

SYNOPSIS

git format-patch []

PARAMETERS

--stdout
    Output all patches to the standard output instead of creating individual files.

-k
    Include the shortlog of the commit in the body of the patch.

-p
    Remove leading slashes from the filenames in the patch.

-N
    Output a complete diff instead of a condensed diff.

--cover-letter
    Generate a cover letter that summarizes the patches.

-o


    Specify the directory where the patch files should be created.


    Specify the range of commits to generate patches for. Examples: HEAD~3..HEAD, master..topic.

DESCRIPTION

The git-format-patch command prepares patches suitable for email submission, applying with git-am, or archiving for later use. It iterates over a range of commits, producing one patch file per commit.

The primary purpose is to extract changesets from a Git repository in a format that can be easily shared with others, particularly those who may not have direct access to the repository or prefer reviewing changesets in a patch format. It's valuable for contributing to open-source projects or sharing modifications within teams where Git access might be restricted. The output files, usually named with sequential numbers and commit summaries, can then be sent through email or other channels.

CAVEATS

The commit range is crucial. Using an incorrect range will lead to incomplete or unintended patches. Be mindful of line endings and whitespace issues, as they can affect patch application. Patches created with git-format-patch do not include information about branch names, so when applying a patch series, you should specify the correct branch to apply to.

COMMIT MESSAGE GUIDELINES

It's crucial that commit messages are clear and descriptive, as they are included in the patch files and serve as documentation for the changes made.

APPLYING PATCHES

Patches created with git format-patch are typically applied using git am. Git am can read mailboxes with multiple patches, or individual patch files.

HISTORY

git format-patch has been a core command since Git's early days, evolving to support a variety of options for tailoring patch output. Its main purpose has always been to facilitate sharing and applying changesets in a portable and human-readable format. It predates web-based pull request systems and remains vital for workflows that rely on email-based patch submission.

SEE ALSO

git am(1), git diff(1), git log(1)

Copied to clipboard