如何使用git子树添加本地回购?

时间:2016-06-12 13:59:49

标签: git git-subtree

假设我有一个已经是git repo的目录" sub",现在我希望它成为我新创建的超级目录的子树" sup"。

我已经搜索过该文档,但所有教程都是关于添加远程仓库或从现有提交中拆分。如何将现有的git repo添加到主git repo?

使用git subtree add --prefix=sub sub会使警告子已存在。

1 个答案:

答案 0 :(得分:2)

根据您的期望,有两种方法可以做到这一点。

  1. 将子仓库添加为子模块。回购保持独立
  2. 添加子仓库作为此子目录的子树。它的历史合并了
  3. 对于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