Search code examples
gitformat-patchgit-amgit-apply

How to 'git-am' apply a patch created with 'git-format-patch --no-prefix'?


I have a reason¹ to create my git commits as patch files using git-format-patch with the --no-prefix option.

This option changes the patch output to not add the git-diff-specific prefixes a/ / b/ in file paths in the patch files. It allows tools like patch to apply the patch files without the need to pass -p1 as argument.

All cool so far. However, it seems that I cannot actually apply them anymore with Git itself (git-am):

$ git am path/to/0001-patch.patch
Applying: <commit message subject>
error: <path>: does not exist in index
Patch failed at 0001
[...]

How do I apply them with git-am now, while maintaining plain patch compatibility?

¹ It allows me to use it as patch files in Bazel without custom patch commands, as you'd need to apply patches with patch -p1 [...].


Solution

  • git-am passes on a few options to git-apply, including the -p option, which does the same as it does with patch. Ie., apply such patches with -p0:

    git am -p0 path/to/0001-patch.patch
    

    It would have been great if git-format-patch was able to hint git-am within the auxiliary data of the patch file created, that it should apply them without the assumption of a path prefix — just like it can hint git-am with base tree information nowadays (see --base option).