373

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,547
  • 2
  • 12
  • 30
silverburgh
  • 8,659
  • 10
  • 31
  • 24

7 Answers7

564

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 --keep-cr --signoff < a_file.patch 

As noted by riverofwind in the comments:

Don't forget if you have autocrlf=false for Windows only development you'll need to pass --keep-cr to am to keep those CRLFs

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

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 7
    `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
  • Also don't forget if you have autocrlf=false for Windows only development you'll need to pass --keep-cr to am to keep those crlfs – riverofwind Jan 06 '23 at 00:40
301
git apply name-of-file.patch
Jeff Dallien
  • 4,491
  • 3
  • 22
  • 19
  • 56
    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
  • 20
    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
69

Or, if you're kicking it old school:

cd /path/to/other/repository
patch -p1 < 0001-whatever.patch
Dominic Cooney
  • 6,317
  • 1
  • 26
  • 38
  • 9
    Just so you're aware: That won't create a commit, so it will lose commit message and author information. – Caesar Jul 27 '19 at 14:29
  • 2
    I've had some cases where this works when `git apply` doesnt, not sure why – Sean Breckenridge Nov 01 '20 at 08:56
  • This is very useful if you are trying to apply a patch to a path or file that is ignored by Git and you have a Git formatted patch. – Brady Jan 02 '21 at 15:45
  • 1
    Indeed, extremely useful when trying to apply a git generated patch on a machine that does not have git installed (docker images for example) – Louis Caron Oct 20 '22 at 06:27
  • Thanks sir! Worked for me when `git apply` just didn't do anything. – liberborn Feb 24 '23 at 16:12
  • @Dominic. Can you do that in a single line? Like `git commit | patch -p0/1` – Amir Mar 02 '23 at 03:44
  • 1
    @Amir Sure. Use a pipe. If you need to change directories, etc. in between consider using [named pipes.](https://linuxconfig.org/introduction-to-named-pipes-on-bash-shell) – Dominic Cooney May 13 '23 at 08:17
44

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
  • 22,193
  • 17
  • 108
  • 158
32

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

Jakub Narębski
  • 309,089
  • 65
  • 217
  • 230
  • 1
    How do you usually get the mbox files in the first place? – Ciro Santilli OurBigBook.com Mar 06 '18 at 03:20
  • or just use `git apply` – Sebi2020 Jan 19 '21 at 09:55
  • 1
    @Sebi2020 : `git apply` applies changes as a patch, not as a commit, while `git am` assumes that the text of the email is the commit message (with some exceptions) and applies changes creating a commit (and it can try to resolve conflicts with 3-way merge with `git am --3way`. – Jakub Narębski Jan 19 '21 at 19:11
  • I cannot get `git am` to work. It stops in the middle applying changes and the commit generated is incomplete – KansaiRobot Jul 04 '21 at 06:21
  • @KansaiRobot : there might be a content conflict when applying changes, but there is not enough information in your comment; please post the problem as a question, with as much detail as possible. – Jakub Narębski Jul 05 '21 at 08:05
26

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,406
  • 4
  • 39
  • 85
  • 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
0

Another way is to add one of the local repositories as a remote to another one.

$ cd repo1
$ git remote add repo2 file:///path/to/repo2/.git

So that you can fetch branches, rebase local branches, or cherry-pick commits from one local repo to another.

$ git remote update repo2
$ git fetch repo2 branch:branch-from-repo2
$ git log branch-from-repo2
MrCryo
  • 641
  • 7
  • 16