As a hack, you can try modifying recipe for comparing commits in two different repositories on GitTips page, i.e.:
GIT_ALTERNATE_OBJECT_DIRECTORIES=../repo/.git/objects \
git cherry-pick $(git --git-dir=../repo/.git rev-parse --verify <commit>)
where ../repo
is path to the other repository.
With modern Git you can use multiple revisions and revision ranges with cherry-pick.
The $(git --git-dir=../repo/.git rev-parse --verify <commit>)
is here to translate <commit>
(for example HEAD
, or v0.2
, or master~2
, which are values in the second repository you copy from) into SHA-1 identifier of commit. If you know SHA-1 of a change you want to pick, it is not necessary.
NOTE however that Git can skip copying objects from source repository, as it doesn't know that the alternate object repository is only temporary, for one operation. You might need to copy objects from the second repository with:
GIT_ALTERNATE_OBJECT_DIRECTORIES=../repo/.git/objects git repack -a -d -f
This puts those objects borrowed from second repository in original repository storage
Not tested.
A not so hacky solution is to follow knittl answer:
- Go to second repository you want to copy commits from, and generate patches from commits you want with
git format-patch
- Optionally, copy patches (0001-* etc.) to your repository
- Use
git am --3way
to apply patches