如何将1个git存储库链接到其他一些存储库?
假设我有以下存储库:
/var/Common.git
/var/Project1.git
/var/Project2.git
现在,我想在其他存储库中使用Common.git.我该怎么做 ?
Cas*_*bel 40
您可能正在寻找子模块:
子模块允许外部存储库嵌入源树的专用子目录中,始终指向特定的提交.
嵌入的一个关键词:Common.git的实际克隆将嵌入其他每个项目中.这通常适用于您不打算在其他项目中修改它,只使用一个版本,并立即从原始Common.git更新该版本.你会做这样的事情:
# add Common.git as a submodule at the path "common" inside this repo
git submodule add /var/Common.git common
# initialize it, clone, and check out a copy
git submodule update --init
# commit the addition of the submodule
git commit
Run Code Online (Sandbox Code Playgroud)
请注意,子模块的路径将提交到您的存储库,因此您应该使用公开的URL.如果要在本地自定义它,可以运行git submodule init,编辑.git/config中的URL,然后运行git submodule update.如果您还有其他问题,请查阅联机帮助页或搜索SO; 这里有很多子模块问题.
另一方面,如果您要在每个项目中编辑Common.git的内容,您可能需要使用git-subtree,它是围绕git的子树合并系统的友好包装器.这将允许您将common.git的内容视为每个项目中的跟踪内容,同时仍然可以将提交分割出来并将它们合并到Common.git本身,并将更新合并到Common.git中. .
这是为其git submodule设计的一个完美案例:http : //git-scm.com/docs/git-submodule
在Project1和Project2中,添加Common的子模块。然后你git submodule checkout
在克隆的仓库中,它仅存储Common git的哈希。所以你git submodule init和结帐。