How to apply a patch generated with git format-patch?
Asked Answered
F

7

394

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?

Fishnet answered 12/2, 2010 at 5:28 Comment(1)
R
591

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

Resentful answered 12/2, 2010 at 7:4 Comment(6)
git am < somepatch.patch yields "fatal: empty ident name (for <>) not allowed". Can someone explain to me why?Organization
@gromit190 that means bad Author headers in the patch, and/or you didn't git config user.{name,email}.Radiobiology
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?Radiobiology
@Radiobiology Maybe with https://mcmap.net/q/12287/-git-apply-fails-with-quot-patch-does-not-apply-quot-error ?Resentful
@Resentful how can i get back, before to apply the patch?Pacheco
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 crlfsMalloch
W
320
git apply name-of-file.patch
Waterside answered 12/2, 2010 at 5:36 Comment(3)
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!Barnebas
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.Tireless
git apply "[...full path to the file...]/name-of-file.patch"Burning
T
74

Or, if you're kicking it old school:

cd /path/to/other/repository
patch -p1 < 0001-whatever.patch
Toothpaste answered 12/2, 2010 at 5:47 Comment(7)
Just so you're aware: That won't create a commit, so it will lose commit message and author information.Abana
I've had some cases where this works when git apply doesnt, not sure whyErudition
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.Knockknee
Indeed, extremely useful when trying to apply a git generated patch on a machine that does not have git installed (docker images for example)Mantis
Thanks sir! Worked for me when git apply just didn't do anything.Mercantilism
@Dominic. Can you do that in a single line? Like git commit | patch -p0/1Spermatophore
@Spermatophore Sure. Use a pipe. If you need to change directories, etc. in between consider using named pipes.Toothpaste
T
47

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

Throw answered 20/9, 2018 at 6:23 Comment(3)
This explanation of the difference helped me to use git apply .. and in --reverse :-) 👍Stonemason
Do you happen to know what “am” is supposed to stand for? Maybe “apply en masse”? ;)Sewing
@fitojb, am may stand for "apply mail", since patches used to be emailed around a lot. See this comment here: What is the difference between git am and git apply?Klinges
D
34

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

Disproportion answered 12/2, 2010 at 12:57 Comment(5)
How do you usually get the mbox files in the first place?Gastrovascular
or just use git applyEthical
@Ethical : 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.Vestment
I cannot get git am to work. It stops in the middle applying changes and the commit generated is incompleteEnrika
@Enrika : 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.Vestment
A
27

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.

Afoul answered 14/5, 2018 at 12:0 Comment(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!Manicotti
A
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
Actinium answered 22/6, 2022 at 12:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.