事实证明,如果您只是试图将两个存储库粘合在一起,并使其看起来像它一直是这样,而不是管理外部依赖关系,那么答案就简单多了。您只需将遥控器添加到旧的回购站,将它们合并到新的主控室,将文件和文件夹移动到子目录,提交移动并重复所有额外的回购。子模块,子树合并和花式重设旨在解决一个稍微不同的问题,并不适合我正在尝试做的事情。
下面是一个例子PowerShell脚本粘上两个储存起来:
# Assume the current directory is where we want the new repository to be created
# Create the new repository
git init
# Before we do a merge, we have to have an initial commit, so we'll make a dummy commit
dir > deleteme.txt
git add .
git commit -m "Initial dummy commit"
# Add a remote for and fetch the old repo
git remote add -f old_a <OldA repo URL>
# Merge the files from old_a/master into new/master
git merge old_a/master --allow-unrelated-histories
# Clean up our dummy file because we don't need it any more
git rm .\deleteme.txt
git commit -m "Clean up initial file"
# Move the old_a repo files and folders into a subdirectory so they don't collide with the other repo coming later
mkdir old_a
dir -exclude old_a | %{git mv $_.Name old_a}
# Commit the move
git commit -m "Move old_a files into subdir"
# Do the same thing for old_b
git remote add -f old_b <OldB repo URL>
git merge old_b/master --allow-unrelated-histories
mkdir old_b
dir –exclude old_a,old_b | %{git mv $_.Name old_b}
git commit -m "Move old_b files into subdir"
很明显,你可以改为合并old_b到old_a(成为合并后的新回购),如果你宁愿做 - 修改适合的脚本。
如果你想带过来正在进行的功能分支为好,使用此:
# Bring over a feature branch from one of the old repos
git checkout -b feature-in-progress
git merge -s recursive -Xsubtree=old_a old_a/feature-in-progress
这过程中唯一的非明显的部分 - 这不是一个子树合并,而是一个参数正常的递归合并告诉Git我们重命名了目标,这有助于Git正确地排列所有东西。
我写了一个稍微更详细的解释here。
我没有使用Git,但是在Mercurial中,如果需要修改要合并的Repos的文件路径,然后强制将一个回购库导入目标以获取更改集,则首先执行转换,然后然后做不同分支的合并。这是测试和工作;)也许这有助于找到Git的解决方案以及...与子树合并方法相比,我猜测转换步骤是不同的,其中历史被重写,而不是仅仅映射路径(如果我理解正确)。这样可以确保顺利合并,而无需任何特殊的文件路径处理。 – Lucero
我也发现这个问题很有帮助http://stackoverflow.com/questions/1683531/how-to-import-existing-git-repository-into-another – nacross
我创建了一个后续问题。可能很有趣:合并两个Git仓库并保留主记录: http://stackoverflow.com/questions/42161910/merge-two-git-repositories-and-keep-the-master-history –