W3docs

How to Use Patch Files in Git

This tutorial will help you solve the problem of generating a git patch for a specific commit. Find the solution by running the command lines step by step.

A patch is a file that contains the changes of one or more commits. It also includes metadata of the commit, such as the commit date, commit message, and so on. You can generate a patch from commits and it will be possible for other developers to apply it to their repositories. It is generally used for code review, mailing lists, or when a developer needs to apply changes without direct write access to the repository.

Generating Patch Files from Commits

To generate a patch file the <kbd class="highlighted">git format-patch</kbd> command is used.

Generating patch files for the given number of commits

If you want to create patches for the last N commits, you can use the <kbd class="highlighted">git format-patch</kbd> command with the <kbd class="highlighted">-N</kbd> option (where N specifies the number of commits you want to create patches for):

generate patches

git format-patch -N

To generate patches for a specific range of commits, you can specify the starting commit followed by ^..HEAD:

generate patches for a range

git format-patch &lt;start-commit-hash&gt;^..HEAD

Generating patch files for the given range of commits

Instead of specifying the number of commits, you can indicate the range of commits to create patches for them. Here is how to do it:

generate patch files for the range of commits

git format-patch &lt;first-commit-hash&gt;^..&lt;end-commit-hash&gt;
Info

To create a single patch file for multiple commits, you should add the --stdout > file.patch to the commands above.

Viewing the changed files inside the patch

To view the changes that have been made inside the patch file, you can use the git apply command with the <kbd class="highlighted">--stat</kbd> option, mentioning the patch file:

show stats git

git apply --stat file.patch

Checking for errors

While applying patches, errors can occur. To avoid them, we recommend you to check for errors before applying a patch, by using the command below:

git apply

git apply --check file.patch

Applying the patch

Finally, to apply the patch file, you should run the command below:

apply the patch

git am < file.patch
Info

git format-patch generates patches in the UNIX mailbox (mbox) format, which is designed to be applied with git am. For standard unified diffs, git apply is typically used instead.

The git format-patch Command

The git format-patch prepares each <kbd class="highlighted">git commit</kbd> with its patch in one file per commit. It is formatted to resemble the UNIX mailbox format. The result of the command is convenient for email submission or for use with git am.

The git apply Command

The git apply command applies a patch to files or the staging area. Reads the supplied diff output and applies it to files. The patched paths outside the directory are ignored when running from a subdirectory in a repository. The <kbd class="highlighted">--cached</kbd> option stages the changes in the index (staging area) instead of modifying the working tree files. Without this option, the patch only updates the working directory files.