How to apply a patch generated with git format-patch?

GitPatch

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

git apply name-of-file.patch

Solution 3 - Git

Or, if you're kicking it old school:

cd /path/to/other/repository
patch -p1 < 0001-whatever.patch

Solution 4 - Git

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

Solution 5 - Git

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

[git-am]: http://www.kernel.org/pub/software/scm/git/docs/git-am.html "git-am(1) Manual Page - Apply a series of patches from a mailbox"

Solution 6 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsilverburghView Question on Stackoverflow
Solution 1 - GitVonCView Answer on Stackoverflow
Solution 2 - GitJeff DallienView Answer on Stackoverflow
Solution 3 - GitDominic CooneyView Answer on Stackoverflow
Solution 4 - GitEugen KonkovView Answer on Stackoverflow
Solution 5 - GitJakub NarębskiView Answer on Stackoverflow
Solution 6 - Gitice1000View Answer on Stackoverflow