如果仓库A和B在同一台机器上,或者在可以相互通信的两台机器上。目标是获取B的一个提交并将其应用于A的一个分支。
cd <path_A>
git checkout <branch_A>
git fetch <path_B> <ref_that_contains_commitB>
git cherry-pick <commitB>
如果A和B位于两台无法直接通信的机器上,此方法也适用于A和B位于同一台机器上。
cd <path_B>
git format-patch <commitB> -1
#a xxx.patch is generated
#move this patch to A's workground.
cd <path_A>
git checkout <branch_A>
git am <path_xxx.patch>
另一种你可能想了解的方法。
cd <path_B>
git bundle create B.bundle <ref_that_contains_commitB>
#B.bundle is generated
#move this bundle to A's workground.
cd <path_A>
git checkout <branch_A>
git fetch <path_B.bundle>
git cherry-pick <commitB>
如果A和B无法直接通信,但他们都可以访问仓库C,可能在github或某些公共服务器上。
cd <path_B>
git push <url_C> <ref_that_contains_commitB>:<ref_in_C>
cd <path_A>
git fetch <url_C> <ref_in_C>
git checkout <branch_A>
git cherry-pick <commitB>
git cherry-pick
命令在某些特定情况下可以替换成
git rebase
或者
git merge
。
更新:
如果提交B修改了一些文件,但是你只想要 file_b 中的改动,
cd <path_B>
git diff <commitB>^ <commitB> -- <file_b> > b.patch
#move b.patch to A's workground
cd <path_A>
git checkout <branch_A>
git apply <path_b.patch>
git add .
git commit -m 'xxx'
或者
cd <path_B>
git format-patch <commitB> -1 -- <file_b>
#xxx.patch is generated
#move xxx.patch to A's workground
cd <path_A>
git checkout <branch_A>
git am <path_b.patch>
git am --abort
并忽略该提交)。它只需要从外部存储库中获取一个单独的提交并将其应用到此处。 - rosuav