假设我有一个已经是git repo的目录" sub",现在我希望它成为我新创建的超级目录的子树" sup"。
我已经搜索过该文档,但所有教程都是关于添加远程仓库或从现有提交中拆分。如何将现有的git repo添加到主git repo?
使用git subtree add --prefix=sub sub
会使警告子已存在。
答案 0 :(得分:2)
根据您的期望,有两种方法可以做到这一点。
对于1,您想使用git子模块。具体地,
在你的sup目录中(已经使用git init初始化),你运行:
git submodule添加sub-sub
它会将子仓库克隆到sup repo中。然后,如果子仓库位于其他地方,则可以将其删除。
请注意,子模块仍然可以作为与顶部存储库不同的存储库。
请参阅子模块的文档:
https://git-scm.com/book/en/v2/Git-Tools-Submodules
对于2,它有点复杂。
首先你获取另一个repo的提交:
# add remote
git remote add sub
# fetch commits
git fetch
# create local branch with sub
git checkout -b sub_branch sub/master
# switch to master
git checkout master
# now, merge commit as a subdirectory
git read-tree --prefix=sub/ -u sub_branch
你可以在将来继续从sub中撤出,它将被合并到sup
- DMG