328

I have two local git repositories, both pointing to the same remote repository.

In one git repository, if I do git format-patch 1, how can I apply that patch to the other repository?

2240
  • 1,570
  • 2
  • 7
  • 23
silverburgh
  • 7,959
  • 10
  • 30
  • 24

6 Answers6

498

Note: You can first preview what your patch will do:

First the stats:

git apply --stat a_file.patch

Then a dry run to detect errors:

git apply --check a_file.patch

Finally, you can use git am to apply your patch as a commit. This also allows you to sign off an applied patch.
This can be useful for later reference.

git am --signoff < a_file.patch 

See an example in this article:

In your git log, you’ll find that the commit messages contain a “Signed-off-by” tag. This tag will be read by Github and others to provide useful info about how the commit ended up in the code.

Example

johv
  • 4,044
  • 3
  • 25
  • 40
VonC
  • 1,129,465
  • 480
  • 4,036
  • 4,755
  • 24
    Note to self: 200th Great Answer gold badge. – VonC May 15 '18 at 18:10
  • 6
    `git am < somepatch.patch` yields "fatal: empty ident name (for <>) not allowed". Can someone explain to me why? – birgersp Sep 03 '19 at 17:28
  • 1
    @gromit190 that means bad `Author` headers in the patch, and/or you didn't `git config user.{name,email}`. – ulidtko Jun 18 '20 at 10:07
  • 1
    OK; `git apply --check` says `patch does not apply`, and `git apply -3` says `repository lacks the necessary blob to fall back on 3-way merge.` In git, rebasing commits is such a breeze; but how do people **rebase their patches** on top of updated code? – ulidtko Jun 18 '20 at 10:10
  • 1
    @ulidtko Maybe with https://stackoverflow.com/a/15375869/6309 ? – VonC Jun 18 '20 at 10:36
  • @VonC how can i get back, before to apply the patch? –  Dec 10 '20 at 14:50
261
git apply name-of-file.patch
Jeff Dallien
  • 4,071
  • 3
  • 21
  • 19
  • 49
    This may not have answered the original detailed question but it answered the question in the title which is why I am on this page. thank you! – Rock Lee Feb 01 '17 at 22:17
  • 17
    I understand this is an old question and answer... but I thought it may be helpful to some people to understand the [difference between git apply and git am](https://stackoverflow.com/questions/12240154/what-is-the-difference-between-git-am-and-git-apply). – mgarey Aug 23 '17 at 17:22
  • 1
    git apply "[...full path to the file...]/name-of-file.patch" – Anton Lyhin Jul 11 '18 at 20:54
58

Or, if you're kicking it old school:

cd /path/to/other/repository
patch -p1 < 0001-whatever.patch
Dominic Cooney
  • 6,050
  • 1
  • 24
  • 38
41

First you should take a note about difference between git am and git apply

When you are using git am you usually wanna to apply many patches. Thus should use:

git am *.patch

or just:

git am

Git will find patches automatically and apply them in order ;-)

UPD
Here you can find how to generate such patches

Eugen Konkov
  • 18,498
  • 11
  • 88
  • 126
30

If you want to apply it as a commit, use git am.

Jakub Narębski
  • 289,171
  • 61
  • 216
  • 230
24

If you're using a JetBrains IDE (like IntelliJ IDEA, Android Studio, PyCharm), you can drag the patch file and drop it inside the IDE, and a dialog will appear, showing the patch's content. All you have to do now is to click "Apply patch", and a commit will be created.

ice1000
  • 6,120
  • 4
  • 35
  • 74
  • 1
    @LasithaBenaragama - kind of. SO is meant to not only help the OP, but also everyone who follows. This answer (while useful) doesn't provide a general solution. This would make an excellent little blog post, but not a "SO good" answer. Would explain the downvote! – OldTinfoil Feb 15 '20 at 16:11