将包含其所有历史记录的git存储库导入现有的git存储库

Cur*_*nny 20 git

我有两个git存储库,我想将它们合并在一起而不会丢失它们的提交历史记录.我试过这个:

cd firstRepo
git remote add other path/to/otherRepo
git fetch other
git checkout -b otherRepoBranch other/master
echo "`git rev-list otherRepoBranch | tail -n 1` `git rev-list master | head -n 1`" >> .git/info/grafts
git rebase otherRepoBranch master
Run Code Online (Sandbox Code Playgroud)

现在,当我查看提交历史记录时,一切看起来都不错,但我存储库中的唯一文件现在是来自otherRepo的文件.

有任何想法吗?

干杯,梅林

Phi*_*eet 11

我认为git存储库是无关的?因为它们是单独项目的不同存储库,您希望将它们合并在一起以形成组合存储库?如果是这样,那么以下引用可能会有所帮助:

组合多个git存储库.

合并两个不相关的存储库.

  • @CuriousAttemptBunny曾经发布过这个解决方案吗? (11认同)

Jes*_*ett 8

在最简单的情况下,您希望在合并后获得两个存储库中的所有文件,您应该能够简单地使用 git merge:

cd firstRepo
git remote add other path/to/otherRepo
git fetch other
git checkout -b merged
git merge --allow-unrelated-histories other/master
Run Code Online (Sandbox Code Playgroud)

  • @CuriousAttemptBunny Merge 永远不会将提交历史记录汇总到单个提交中。您一定将它与另一种方法混淆了。 (3认同)

nob*_*bar 5

这篇权威文章讨论了开始段落中的简单情况-并以更可能的情况作为其主要主题:如何使用子树合并策略

两个存储库的提交历史记录都将保留。


这是我的版本-根据上述参考文章...

git remote add temp staging_path/(reponame)
git fetch temp
git fetch --tags temp ## optional -- may pull in additional history
for remote in $(git branch -r | grep temp/ ) ; do git branch --no-track imported_$(basename $remote) $remote ; done ## create local branches prefixed with 'imported_'
git remote rm temp ## optional -- assumes you no longer plan to use the source repo

git merge -s ours --no-commit imported_master ## mysterious "merge strategy"
git read-tree -u --prefix=(reponame)/ imported_master ## move to sub-folder
git commit
Run Code Online (Sandbox Code Playgroud)