ZANARDI Bruno

Software Developper

About
Resume

Git Blog

How To: Patches

home next

related commands:
am apply format-patch

Create a patch

You cloned a git repository, modified the source code, made some commits and for a reason or an other you can’t / don’t want to push them but you want to share them with the project maintainer? Build a patch and send it to him (or her) by email!

The basic command is git format-patch, and here is the options you can use.

You know how much commits you want to share

$ git format-patch -{x} HEAD --stdout > <name>.patch

Replace {x} by the number of commits. Git will create a patch which contains the {x} last commits starting from HEAD included.

You know your first commit hash

$ git format-patch <hash>^ --stdout > <name>.patch

Git will create a patch which contains all the commits since the parent (excluded) of your first commit

You did your commits in a branch

$ git format-patch master --stdout > <name>.patch

Git will create a patch which contains all the commits since your branch start.

These three commands will create a file in the current directory you can send to the project maintainer.

Check a patch

You created a patch or you received a patch by email, and you want to check it before sending / apply it?

Run this command:

$ git apply --stat <name>.patch

It will show you the same output than git diff --stat.

You also can check if this patch is applicable to your working directory? Also use git apply command, but with the --check option:

$ git apply --check <name>.patch

It will tell you if you will be able to apply the patch without conflicts.

Apply a patch

Yes, we can use apply to apply the patch, but we won’t. Instead, we’ll use am. Why? Because am has the --signoff option which will put the name and the email of the patch maker in the commit identification.

$ git am --signoff -k < <name>.patch

The -k option is used to keep the text between [] in the patch commits messages.

home top next

Reminder

am

Apply patches from a file with preserving author informations and messages

$ git am --signoff -k < <name>.patch

apply

Check a patch content

$ git apply --stat <patch>

Check if a patch is applicable

$ git apply --check <patch>

format-patch

Create a patch with the {x} last commits

$ git format-patch -{x} HEAD --stdout > <name>.patch

Create a patch with commits since the parent of <hash> (excluded):

$ git format-patch <hash>^ --stdout > <name>.patch

Create a patch with commits since branch start:

$ git format-patch <parent_branch> --stdout > <name>.patch
home top next