19

I'd need to create simple patches from git repository that can be applied with plain simple patch command line utility.

Can it be done?

Nikola Kotur
  • 1,954
  • 3
  • 13
  • 15

2 Answers2

21

Patches, that git diff yields, are correctly processed by patch tool.

patch skips all the additional info git appends to the patch file. To apply the patch you most likely will need -p1 option.

P Shved
  • 96,026
  • 17
  • 121
  • 165
  • 3
    Patches generated by `git format-patch` are also correctly procesed by GNU patch... unless they are generated with -M / -C option and contain information about renames, etc. – Jakub Narębski Oct 20 '09 at 14:07
  • 3
    From the `patch` man page: "patch tries to skip any leading garbage, apply the diff, and then skip any trailing garbage." The big unique thing in git diffs is the addition of modelines (e.g. "index...", "rename...", "new file...") and patch is able to simply skip these. Git also prefixes filenames, e.g. "a/path/to/file", but this can be skipped by patch's -p1. – Cascabel Oct 20 '09 at 14:54
  • Is there a way to generate a patch that can be proceeded by 'patch -p0'? I am applying my own patches to some program for macports but macports applies patches using -p0. – Kan Li Feb 07 '12 at 04:44
  • 2
    @icando, did you try `--no-prefix` option? – P Shved Feb 08 '12 at 08:53
  • Thanks. The `--no-prefix` option did the trick. Otherwise default generated diffs are incompatible with `patch`. – coladict Aug 25 '16 at 15:06
4

The accepted answer states the following:

Patches, that git diff yields, are correctly processed by patch tool.

I'm pretty sure I just ran into a case where this is incorrect. /usr/bin/patch just silently (without reporting an error) ignored my patch including file rename information, thereby breaking a deployment (fortunately I'm only testing the deployment at the moment :-) ...

I'm posting this alternative answer as a heads up for other people running into the same problem because I was scratching my head for a while... Also comments to answers on StackOverflow apparently can't contain quotes.

Ironically enough I just now switched to the unified diff format to overcome this problem and now my deployment breaks in a different way because unified diffs can't represent the creation of empty files (e.g. __init__.py). Talk about being between a rock and a hard place!

xolox
  • 4,888
  • 3
  • 24
  • 15