【问题标题】:Why does adding an existing repo as a submodule modify .git/config?为什么将现有 repo 添加为子模块会修改 .git/config?
【发布时间】:2011-05-21 17:36:04
【问题描述】:

如果我添加一个当前不存在的子模块,.git/config 不会添加任何子模块信息。

$ mkdir testing
$ cd testing
$ git init
$ git submodule add git@git.server:submodule.git
$ cat .git/config 
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true

但是,如果我添加一个当前作为子模块存在的 repo,则会将 url 添加到 .git/config:

$ mkdir testing
$ cd testing
$ git init
$ git clone git@git.server:submodule.git
$ git submodule add git@git.server:submodule.git
$ cat .git/config 
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
[submodule "submodule"]
    url = git@git.server:submodule.git

我会假设在这两种情况下,git submodule add 只会修改.gitmodules,而git submodule init 会更新项目的.git/config

为什么在第二种情况下修改了.git/config 而不是第一种情况?有人可以解释这种行为的原因吗?

【问题讨论】:

    标签: git add git-submodules


    【解决方案1】:

    这看起来确实很奇怪。这种行为是在in this commit 引入的:

    commit c2f939170c65173076bbd752bb3c764536b3b09b
    Author: Mark Levedahl <mlevedahl@gmail.com>
    Date:   Wed Jul 9 21:05:41 2008 -0400
    
        git-submodule - register submodule URL if adding in place
    
        When adding a new submodule in place, meaning the user created the
        submodule as a git repo in the superproject's tree first, we don't go
        through "git submodule init" to register the module.  Thus, the
        submodule's origin repository URL is not stored in .git/config, and no
        subsequent submodule operation will ever do so.  In this case, assume the
        URL the user supplies to "submodule add" is the one that should be
        registered, and do so.
    
        Signed-off-by: Mark Levedahl <mlevedahl@gmail.com>
        Signed-off-by: Junio C Hamano <gitster@pobox.com>
    

    更新:您在下面的 cmets 中指出,我对该提交消息的原始解释没有任何意义,因此我现在删除了该文本以避免对其他人造成混淆。

    正如下面的 cmets 所述,cdwilson 发布到 git 邮件列表以询问这种不一致,因此 Jens Lehman 正在努力修复 - 可以在此处找到该线程:

    【讨论】:

    • 马克,感谢指向该提交的指针(至少它表明这种行为是故意的)。但是,当您说“在更新子模块之前不会克隆它”时,我仍然感到困惑。在上面的第一种情况下,git submodule add git@git.server:submodule.git 实际上将submodule.git 克隆到超级项目中。在上面的示例中,我运行的唯一子模块命令是git submodule add,在这两种情况下,我最终都在我的超级项目中克隆了一个 submodule.git。唯一不同的是.git/config之后的样子,案例#1也需要git submodule init注册。
    • 如果在第一种情况下,git submodule add 在子模块注册 (git submodule init) 和更新 (git submodule update) 之前没有克隆存储库,那么这种行为对我来说是有意义的。但是,由于上面的案例#1 实际上确实克隆了submodule.git,并且需要git submodule init 进行注册,我希望案例#2 也需要git submodule init 来执行注册。我确定这里只是缺少一些东西,但现在我不明白为什么会有差异。
    • @cdwilson:很好,我在急于回答问题时记错了git submodule add 的行为。我会更正我的答案。你可以在 git 邮件列表上询问这个问题以获得正确的答案 - 我怀疑这是一个错误,但也许我遗漏了一些东西。
    【解决方案2】:
    猜你喜欢
    • 2020-07-08
    • 1970-01-01
    • 2023-03-29
    • 2016-09-30
    • 2020-12-20
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 2013-01-20
    相关资源
    最近更新 更多