306

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.

2
  • 4
    Anyone know how to do this if the patch includes renames? Does patch support that natively now? Commented Aug 24, 2011 at 14:56
  • 3
    The question should really be: is there a way to apply a git diff without git installed? As noted below, patch doesn't fully support this format. Commented Sep 29, 2013 at 16:48

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

12 Comments

Or use git diff > patchfile, but patch -p1 < patchfile
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
patch -p1 < patchfile does not require git installed. The first command demonstrates command for generating diff, not applying it.
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; ...
@PaulChechetin As egor83 said in suppie's answer it strips slash in the beginning.
|
79

try this:

patch -p1 < patchfile

3 Comments

What does the -p1 argument do?
Strips slash in the beginning. See man patch
@chrisjlee git diff will put a/ and b/ prefixes in the output, so patch -p1 neglects those to apply the patch file.
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.

3 Comments

Yes, git apply is the best way to do it, but this question specifically asks how to apply the patch without Git installed.
Options --dry-run --verbose are useful to determine what the side effects will be, if any. (using patch v2.5.8)
@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.
9

I use

patch -p1 --merge < patchfile

This way, conflicts may be resolved as usual.

Comments

-27

Try this:

$ git apply file.diff

1 Comment

See question: "without git installed"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.