您可以执行历史移植以允许您将历史从“旧”存储库移动到新存储库。
假设您想从foo
存储库 X 中的分支获取更改,并将其应用于存储库 Y 中的 master。首先,让我们将 X 作为远程添加到 Y,以便我们可以操作两个历史记录:
git remote add tmp-X ~/repository-X
git fetch tmp-X
既然这两个历史都在一个存储库中,我们就可以挑选来进行我们想要的更改。假设历史如下所示master
:
commit 0d39ac6df9f5d6e7b50d670d705fa4aae049b70d
Author: Joe User <user@example.com>
Date: Sat Oct 13 03:55:24 2012 -0400
Common parent commit
并在tmp-X/master
:
commit be79703c19475118b72655d1ea5d1957f3edea58
Author: Joe User <user@example.com>
Date: Sat Oct 13 03:57:29 2012 -0400
Another change I want to keep
commit 2a1e95c6cb458718448581664d9f8e6a0b260968
Author: Joe User <user@example.com>
Date: Sat Oct 13 03:56:00 2012 -0400
Change I want to keep
commit 0fdda21de3382acb005e9511e6ce95007f20e687
Author: Joe User <user@example.com>
Date: Sat Oct 13 03:55:24 2012 -0400
Common parent commit
[some kind of junk in the commit message that changes the IDs]
那么我们可以git checkout master
:
git cherry-pick 0fdda21..tmp-X
现在master
看起来像:
commit b2246afa547ee52028331d2aa59400bca1265581
Author: Joe User <user@example.com>
Date: Sat Oct 13 03:57:29 2012 -0400
Another change I want to keep
commit da36f4b6728aecfd3da7c63d9b1bd430c864858b
Author: Joe User <user@example.com>
Date: Sat Oct 13 03:56:00 2012 -0400
Change I want to keep
commit 0d39ac6df9f5d6e7b50d670d705fa4aae049b70d
Author: Joe User <user@example.com>
Date: Sat Oct 13 03:55:24 2012 -0400
Common parent commit
如果您在主线中进行了其他更改,您可能还会发现从mainline
对应于基本提交(0fdda21
上面)的历史提交中分支出来,挑选到该分支,然后将此临时分支合并到主线中会很有帮助。