302

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.

Ryan
  • 3
  • 2
Andrey Kuznetsov
  • 11,640
  • 9
  • 47
  • 70
  • 4
    Anyone know how to do this if the patch includes renames? Does patch support that natively now? – Paul Crowley Aug 24 '11 at 14:56
  • 3
    The question should really be: *is there a way* to apply a git diff without git installed? [As noted below](http://stackoverflow.com/a/17958129/519015), `patch` doesn't fully support this format. – Aryeh Leib Taurog Sep 29 '13 at 16:48

5 Answers5

452
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.

Tim
  • 12,318
  • 7
  • 50
  • 72
Andrey Kuznetsov
  • 11,640
  • 9
  • 47
  • 70
  • 99
    Or use `git diff > patchfile`, but `patch -p1 < patchfile` – Jakub Narębski Aug 05 '10 at 21:18
  • 12
    If you want to create a patchfile of a subpath of the repository you can use the `relative` option like: `git diff --no-prefix --relative=my/relative/path > patchfile` – Koen. Jul 02 '12 at 17:28
  • Doesn't this require git to be installed? The question is how to do it without git right? – UpTheCreek Apr 24 '13 at 08:53
  • 3
    `patch -p1 < patchfile` does not require git installed. The first command demonstrates command for generating diff, not applying it. – Andrey Kuznetsov Apr 24 '13 at 10:02
  • 1
    The patch generated is for the changes **from** the branch/refspec indicated in the command **to** the current or active branch. In other words, you want `git diff from_branch > patchfile; git checkout from_branch; git patch -p1 < patchfile` or `git diff from_branch to_branch > patchfile; ...` – hobs May 08 '13 at 21:57
  • @AndreyKouznetsov That works! Could you explain what does `-p1` mean? – Paul Brit Sep 23 '13 at 14:31
  • 1
    @PaulChechetin As egor83 said in suppie's answer it strips slash in the beginning. – Andrey Kuznetsov Sep 23 '13 at 15:03
  • As noted in [Sola Yang's answer](http://stackoverflow.com/a/17958129/519015), `patch` does not fully understand the git diff format, so this is unfortunately not a complete solution. It might work most of the time, but eventually a case will arise in which it doesn't work. You should either get the diff in a fully supported format or use git to apply the diff. – Aryeh Leib Taurog Sep 29 '13 at 16:43
  • 1
    Even with the -p1, my patch utility on Solaris asks for a file to patch. There are no renames or anything, this is just a simple patch file. This solution does NOT work for me. – Ben Nov 14 '13 at 01:56
  • 1
    I guess things have changed (for better): the proposed solution works for me, even if creating, renaming and deleting files. (I'm using `git version 1.8.3.2` from package `dev-vcs/git` and `patch 2.6.1` from package `sys-devel/patch` in Gentoo.) – Alberto Jan 15 '14 at 15:21
  • not even git understand its own diff format. try `git diff 123..123 | git apply` use proper commit ids. it will complain about everything. even blank line at EOF. – gcb Sep 17 '14 at 19:58
  • 1
    Question was "How to apply `git diff` patch _without_ Git installed?" and answer is "git" seriously?? – Dainius Nov 19 '15 at 03:15
78

try this:

patch -p1 < patchfile
Oleks
  • 31,955
  • 11
  • 77
  • 132
suppie
  • 1,087
  • 8
  • 10
51

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.

0mid
  • 149
  • 5
Sola Yang
  • 975
  • 8
  • 8
  • 1
    +1, The only sane answer. Moreover, diff/patch won't handle symlinks, which is a problem if (for example) you are reverting the 3.10 Linux kernel patch. – ignis Aug 04 '13 at 22:37
  • 11
    Yes, `git apply` is the best way to do it, but this question specifically asks how to apply the patch *without Git installed*. – Colin D Bennett Oct 22 '13 at 19:30
  • 1
    Options `--dry-run --verbose` are useful to determine what the side effects will be, if any. (using patch v2.5.8) – spyle Feb 29 '16 at 15:59
  • @ignis - *"`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. – jww Jun 18 '16 at 14:27
9

I use

patch -p1 --merge < patchfile

This way, conflicts may be resolved as usual.

denis.peplin
  • 9,585
  • 3
  • 48
  • 55
-25

Try this:

$ git apply file.diff
jww
  • 97,681
  • 90
  • 411
  • 885
Shashi
  • 2,137
  • 3
  • 22
  • 37