How can my client apply patch created by git diff without git installed?
I have tried to use patch command but it always asks file name to patch.
5 Answers
git diff > patchfile
and
patch -p1 < patchfile
work but as many people noticed in comments and other answers patch does not understand adds, deletes and renames. There is no option but git apply patchfile if you need handle file adds, deletes and renames.
EDIT December 2015
Latest versions of patch command (2.7, released in September 2012) support most features of the "diff --git" format, including renames and copies, permission changes, and symlink diffs (but not yet binary diffs) (release announcement).
So provided one uses current/latest version of patch there is no need to use git to be able to apply its diff as a patch.
12 Comments
git diff > patchfile, but patch -p1 < patchfilerelative option like: git diff --no-prefix --relative=my/relative/path > patchfilepatch -p1 < patchfile does not require git installed. The first command demonstrates command for generating diff, not applying it.git diff from_branch > patchfile; git checkout from_branch; git patch -p1 < patchfile or git diff from_branch to_branch > patchfile; ...try this:
patch -p1 < patchfile
3 Comments
git diff will put a/ and b/ prefixes in the output, so patch -p1 neglects those to apply the patch file.Use
git apply patchfile
if possible.
patch -p1 < patchfile
has potential side-effect.
git apply also handles file adds, deletes, and renames if they're described in the git diff format, which patch won't do. Finally, git apply is an "apply all or abort all" model where either everything is applied or nothing is, whereas patch can partially apply patch files, leaving your working directory in a weird state.
3 Comments
git apply is the best way to do it, but this question specifically asks how to apply the patch without Git installed.--dry-run --verbose are useful to determine what the side effects will be, if any. (using patch v2.5.8)git apply patchfile - ... the only sane answer..." - that's almost laughable. Every time the OpenSSL devs send me a patch to test, Git fails to apply it. That's every time. I've yet to see that stupid tool apply a patch.Try this:
$ git apply file.diff
patchdoesn't fully support this format.