The format-patch command requires an identifier to tell Git what the first patch should be. Git will create as many patch files as it needs to change code from what it was then to what it should be.
There are several ways to identify the starting snapshot. One common use for a set of patches is to submit the changes you've made to a given branch to the package maintainer.
For example, suppose you've created a new branch off the master for a new feature. When you've completed your testing, you may send a set of patch files to the project maintainer so they can validate your work and merge the new feature into the project.
The format-patch sub-command with the name of a parent branch will generate the patch file to create your current branch:
$ git checkout master
$ git checkout -b newFeature
# Edits, adds and commits.
$ git format-patch master
0001-Patch-add-new-feature-to-menu.patch
0002-Patch-support-new-feature-in-library.patch
Another common identifier is a git snapshot SHA1. Each git snapshot is identified by an SHA1 string.
You can view a log of all the commits in your repository with the git log command:
$ git log
commit 82567395cb97876e50084fd29c93ccd3dfc9e558
Author: Clif Flynt <clif@example.com>
Date: Thu Dec 15 13:38:28 2016 -0500
Fixed reported bug #1
commit 721b3fee54e73fd9752e951d7c9163282dcd66b7
Author: Clif Flynt <clif@example.com>
Date: Thu Dec 15 13:36:12 2016 -0500
Created new feature
The git format-patch command with an SHA1 identifier looks like this:
$ git format-patch SHA1
You can use a unique leading segment of the SHA1 identifier or the full, long string:
$ git format-patch 721b
$ git format-patch 721b3fee54e73fd9752e951d7c9163282dcd66b7
You can also identify a snapshot by its distance from your current location with a -# option.
This command will make a patch file for the most recent change to the master branch:
$ git format-patch -1 master
This command will make a patch file for the two most recent changes to the bleedingEdge branch:
$ git format-patch -2 bleedingEdge
Applying a patch
The git apply command applies a patch to your working code set. You'll have to check out the appropriate snapshot before running this command.
You can test that the patch is valid with the --check option.
If your environment is correct for this patch, there will be no return. If you don't have the correct branch checked out, the patch -check command will generate an error condition:
$ git apply --check 0001-Patch-new-feature.patch
error: patch failed: feature.txt:2
error: feature.txt: patch does not apply
When the --check option does not generate an error message, use the git apply command to apply the patch:
$ git apply 0001-Patch-new-feature.patch