Merging one file from one branch to another has been asked before with the rather concise solution to do:
git checkout A
git checkout --patch B f
But how does one do this effectively across two repositories?
Merging one file from one branch to another has been asked before with the rather concise solution to do:
git checkout A
git checkout --patch B f
But how does one do this effectively across two repositories?
There is a simple solution, too and it involves patching. git can handles patches sent in by email very well. That is one of the important initial use cases by Linus Torvalds. So we first generate such a patchfile in the source repo, then consume it in the destination:
cd source_repository/
git log --reverse --follow -p --format=email filename > patch
cd other_repository/
git am --whitespace=nowarn source_repository/patch
rm source_repository/patch
Some explanation:
git am
stands for “git apply mailbox file” and is used to integrate patches that someone sends by emailgit log –p
can create such a patch file with option --format=email
--follow
is used to keep track of the full history of the file even if it got renamed. --follow
prevents git log
from accepting multiple file names. But it is an important switch and that is why I recommend to use this method in a loop for multiple files.--reverse
is required because git am
requires the commits to be in chronological order with git log
by default does not output.git am
warns about lines with trailing whitespaces. --whitespace=nowarn
suppresses those warnings. You might want to not use this and clean you whitespaces, but that's a matter of style guidelines