279

I work on WordPress based project and I want to patch my project at each new release version of WP. For this, I want generate a patch between two commits or tags.

For example, in my repo /www/WP I do this:

$ git patch-format com1..com2 --stdout > '~/patchs/mypatch.patch'

# or

$ git patch-format tag1..tag2 --stdout > '~/patchs/mypatch.patch'

/www/WP git natif WordPress

/www/myproject My git project WordPress based

The git apply command line doesn't work, I think because we are in different repositories.

Can I generate a patch file without a commit, just a differential and apply it to another git repository?

6 Answers 6

411

You can just use git diff to produce a unified diff suitable for git apply:

git diff tag1..tag2 > mypatch.patch

You can then apply the resulting patch with:

git apply mypatch.patch
9
  • 1
    Thank you Enrico, I used $git diff -u tag1..tag2 > mypatch.patch and $git apply --stat > mypatch.patch the answer is 0 files changed any other suggestion please ? :)
    – zatamine
    Jan 28, 2015 at 13:48
  • You need to specify the path to the patch file as an argument of git apply. I updated my answer with an example. Jan 28, 2015 at 23:05
  • 5
    I used git diff -p tag1 tag2 > my.patch which worked well.
    – barclay
    Jan 19, 2018 at 20:30
  • How do I make patches in unified diff format? sourceware.org/glibc/wiki/… says Only unified diff (-uNr) format is acceptable. but when I try git diff -uNr tag1..tag2 > mypatch.patch I get the message usage: git diff [<options>] [<commit> [<commit>]] [--] [<path>...] Apr 23, 2018 at 5:22
  • 6
    Be careful git diff ... + git apply ... do not handle deleted / moved files properly ... when git format-patch ... + git am ... do. Jan 8, 2020 at 13:32
74

To produce patch for several commits, you should use format-patch git command, e.g.

git format-patch -k --stdout R1..R2

This will export your commits into patch file in mailbox format.

To generate patch for the last commit, run:

git format-patch -k --stdout HEAD~1

Then in another repository apply the patch by am git command, e.g.

git am -3 -k file.patch

See: man git-format-patch and git-am.

7
  • 1
    What about a patch that can be applied with patch -p1? sourceware.org/glibc/wiki/… Apr 23, 2018 at 5:23
  • I was moving patch from linux to winodws, so had to use --ignore-whitespace as mentioned here stackoverflow.com/questions/13190679/…
    – Mahesh
    Jun 6, 2018 at 10:46
  • 2
    @AaronFranke, you can try '-p' option: git format-patch -p HEAD^1
    – Piroxiljin
    Jan 16, 2019 at 6:29
  • Of note, this has the advantage of keeping the commit messages, author, & commit dates of the original commits.
    – M. Justin
    Feb 27, 2020 at 16:07
  • 1
    Also, kindly explain the options -3 and -k in your answer. Personally I don't think -k is necessary here but I'm not too sure about -3.
    – ADTC
    Oct 14, 2021 at 12:38
52

You can apply two commands

  1. git diff --patch > mypatch.patch // to generate the patch`
  2. git apply mypatch.patch // to apply the patch`
3

As a complementary, to produce patch for only one specific commit, use:

git format-patch -1 <sha>

When the patch file is generated, make sure your other repo knows where it is when you use git am ${patch-name}

Before adding the patch, use git apply --check ${patch-name} to make sure that there is no confict.

2

You even can do patches only for sub directory you are currently in. Just add .

git format-patch -k b365cce8..80a2c18a .

Then you can apply them:

git am *.patch

See this answer for details

1

Make use of this command.

git diff master..localbranch > file.patch

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.