views:

2924

answers:

3

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?

+4  A: 

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".

Henrik Paul
Hmm, having read through the submodule docs, this doesn't sound like "an easy way" though it may well be the most robust.It looks like I'd have to create a submodule containing just the `playdar.js` file then include that in both other projects (I don't want everything else from `www.playdar.org` in the `playlick.com` project)I might just resort to manually editing the patch files for now to be honest. Or continue to copy paste between the two.Cheers.
James Wheare
Here's a clear and thorough tutorial and getting started with git submodule for anyone else stumbling upon this question: http://book.git-scm.com/5_submodules.html
James Wheare
+2  A: 

To complete Henrik's answer, and to go for the bonus point

what if the file you want to apply the patch to isn't in a git repository?

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.

VonC
+5  A: 

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:

diff --git a/lib/playdar.js b/lib/playdar.js
index 1234567..89abcde
-- a/lib/playdar.js
++ b/lib/playdar.js

All you have to do is change "lib/playdar.js" to "static/playdar.js" and then run the patch through "git am".

The patch should be readable by the standard GNU patch utility for people who don't have git--- but don't run format-patch with the "-M", "-C" etc. options to produce rename patches in that case, because the support for them isn't universal.

araqnid
Revisiting this page later… This is a better answer to the question asked than the previous "winner" that suggested submodules.
James Wheare