与git子模块共享一个工作树

Jed*_*Jed 5 git git-submodules

假设我有一个库Common,可以使用独立的,并且所使用的项目P1P2,因此,树我想要的样子

/Common/.git
        ...
/P1/.git
    .gitmodules  # points to remote server
    Common/
    ...
/P2/.git
    .gitmodules  # points to remote server
    Common/
    ...
Run Code Online (Sandbox Code Playgroud)

当我进行更改时/Common,我希望能够在提交之前P1P2之前测试它.使用通常的git submodule命令集,我将不得不提交/Common,推送到远程,然后从两个/P1/Common/P2/Common.如果提交中断了某些内容,则无法修改,因为已经发布了错误的更改.或者,我可以git remote add quicktest /Common/P?/Common不触摸远程服务器的情况下拉动.但是这有很多不一致的机会,并且删除破坏的提交是很脏的,/P?/Common以便可以修改它们/Common.

我宁愿认为,在开发过程中,从工作树/Common被使用P1P2,但我不能让/P1/Common一个符号链接/Common,因为git submodule识别符号链接从目录不同.大多数文件系统不允许使用硬链接目录.我可以使用硬链接所有文件

rm -rf /P1/Common
cp -rl /Common /P1/Common
Run Code Online (Sandbox Code Playgroud)

/Common在添加新文件之前效果很好,在这种情况下需要重复此过程.两者都有优雅的方式吗?

  1. 继续git clone --recursive git://remote/P1.git为最终用户工作,并且
  2. 允许我轻松测试/Common工作中的变化P1P2

Von*_*onC 0

我宁愿建立一个中间裸存储库,以便:

  • 推送Common新的提交
  • 从 forP1/Common和 中拉出P2/Common

至少,如果稍后修改/删除提交,则该中间存储库从未在外部发布,并且您仍然可以将您的模块P1/CommonP2/Common子模块重置为该中间存储库内容。


更新:如果您正在使用git worktreeGit 2.5(2015 年 7 月,写此答案 5 年后),请确保使用Git 2.25 (Q1 2020)
在此之前,“ git worktree add”内部调用了“ reset --hard”,影响了子模块。