I have two repositories, one is the main repo for a library, and the other is a project using that library.
If I make a fix to the in the subservient project, I'd like an easy way to apply that patch back upstream.
The file's location is different in each repository.
- Main repo:
www.playdar.org/static/playdar.js
- Project:
playlick.com/lib/playdar.js
I tried using git format-patch -- lib/playdar.js
on the playlick project, and then git am
on the main playdar repo, but the differing file locations in the patch file raised an error.
Is there an easy way to apply the patch from a given commit on a given file to another arbitrary file elsewhere?
For bonus points, what if the file you want to apply the patch to isn't in a git repository?
If manually editing the patch file is out of the question or infeasible, this can be done with standard options (available in
git apply
,git format-patch
and GNUpatch
).-p<n>
removesn
leading directories from the paths in the patch.After processing
-p
,--directory=<root>
prependsroot
to each of the paths in the patch before applying.Example
So, for your example, to take a patch that was originally on
static/playdar.js
and apply it tolib/playdar.js
, you would run:The patch produced by
git format-patch
is simply a text file-- you can edit the diff headers so that it modifies a different path.So for instance it would have produced something like this:
All you have to do is change
lib/playdar.js
tostatic/playdar.js
and then run the patch throughgit am"
The patch should be readable by the standard GNU patch utility for people who don't have
git
--- but don't runformat-patch
with the-M
,-C
etc. options to produce rename patches in that case, because the support for them isn't universal.