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?
You can just remove (rename) temporarily the main repository.
Assuming both projects are git projects, it sounds like that submodules would be the perfect fit for you. This allows a git project dynamically link to another git project, essentially baking a git repo right inside another git repo, both having their own distinct lives.
In other words, add "main repo" as a submodule in "project". Whenever you commit/push new stuff in "main repo", you just
git pull
them back into "project".You can add a new remote and pull from it. Article with details.
I think subtree is the best solution for your problem
Tutorial 1
Tuorial 2
To complete Henrik's answer, and to go for the bonus point
If you have access to the directories of the file candidate for a patch coming from a git repository, you could first transform that tree of directories/files into a git repository itself! ('
git init
': a git repository is just a .git within a root directory after all).Then you would set that repo as a submodule for your main project.
Using the
--relative
option toformat-patch
can improve the abstraction (hide irrelevant details about the repository from which the patch was generated).I have found the
--3way
option to be required when applying the patch (to avoiddoes not exist in index
error) -- your mileage may vary. Using--directory=(...)
is likely only necessary if your target path is not the root of the repository.format-patch
will create one patch file per commit to the current branch since 'base'.The documentation for the
--relative
option seems to be missing in some cases, but it appears to work anyway (as of version 2.7.4).