【发布时间】:2016-06-12 13:59:48
【问题描述】:
假设我有一个已经是 git repo "sub" 的目录,现在我希望它成为我新创建的超级目录 "sup" 的子树。
我已经搜索了文档,但所有教程都是关于添加远程仓库或从现有提交中拆分的。如何将现有的 git repo 添加到主 git repo?
使用git subtree add --prefix=sub sub 会给出警告子已经存在。
【问题讨论】:
标签: git git-subtree
假设我有一个已经是 git repo "sub" 的目录,现在我希望它成为我新创建的超级目录 "sup" 的子树。
我已经搜索了文档,但所有教程都是关于添加远程仓库或从现有提交中拆分的。如何将现有的 git repo 添加到主 git repo?
使用git subtree add --prefix=sub sub 会给出警告子已经存在。
【问题讨论】:
标签: git git-subtree
有两种方法可以做到这一点,具体取决于您的期望。
对于 1,你想使用 git 子模块。具体来说,
在你的 sup 目录(已经用 git init 初始化)你运行:
git 子模块添加子位置
它会将 sub repo 克隆到 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
【讨论】: