Table of Contents

Patching

Patching in git is rather straightforward, though the commands are not all that obvious and the man pages are not terribly intuitive. Patching is used to move commits from one repository to another indirectly.

Creating Patches

To create a patch, use git format-patch. It will spit out a series of patch files with one commit in each patch. Examples:

Only the newest commit:

git format-patch HEAD^

The last three commits:

git format-patch HEAD~3

The 4th through 2nd last commit:

git format-patch -3 HEAD~4

Only the commit deadbeef…

git format-patch -1 deadbeef

The last 10 commits, but without modifying the commit messages:

git format-patch -k HEAD~10

Checking Patches

Patches do not always apply correctly. Before using git am to apply a patch, it is always a good idea to check it first with git apply.

To see what a patch will change, run:

git apply --stat 0001-mypatch.patch

To see how a patch will apply, run:

git apply --check 0001-mypatch.patch

If you're confident the patch will apply and isn't changing anything strange, then apply it with git am.

Applying Patches

The easiest way to apply patches is with git am. To apply the patch 0001-mypatch.patch, run

git am 0001-mypatch.patch

To sign off patches as they are committed, use -s like so:

git am -s 0001-mypatch.patch

To fudge the commit date to be the same as the author date, use –committer-date-is-author-date like so::

git am --committer-date-is-author-date 0001-mypatch.patch