Patching with Git

Here's a quick tip which, like most things on this blog, are here because I at one point or another had trouble finding a resource for them.

If you want to create a patch with git that can be applied with the common UNIX/Linux patch command, it's super-simple:

git diff --no-prefix --no-ext-diff -w > cool_updates.patch


  • --no-prefix excludes the a/ and b/ that are otherwise automatically appended to the paths in the patch file.
  • --no-ext-diff ignores any settings in your gitconfig which you've configured to open a GUI diff tool, such as meld.
  • -w tells git to ignore differences that are only whitespace. This makes the patch cleaner and often much shorter if you clean up old code as you work around it, like you should.

I use this so frequently that I've set it up as an alias in by .bashrc:

alias git_patch="git diff --no-prefix --no-ext-diff -w $1"

Now when I type:

git_patch master > /tmp/my_change.patch

I get a file that's a diff between my current branch and master.

To apply this patch in another branch:

patch -p0 < /tmp/my_change.patch

NOTE: You can create a patch from anywhere in your repository. However, you must apply it from the root of your repository. Otherwise patch will think it has to create all the subfolders in the patch.

If you use the .patch or .diff file extension you will get the benefit of syntax highlighting in a variety of applications, from Trac to the simple gedit.

Comments !