【问题标题】:How to change git submodules url locally?如何在本地更改 git 子模块 url?
【发布时间】:2017-06-21 01:43:57
【问题描述】:

原始的.gitmodules 文件使用硬编码的https url,但对于一些自动化测试,我从ssh 克隆,并使子模块url 与../ModuleName 中的相对应。我也不想将这些更改推回 repo。

# change the https://github.com/ with git@github.com:
sed -i 's/https:\/\/github.com\//git@github.com:/g' ".git/config"
# delete the url lines from the submodule blocks of .git/config
sed -i '/submodule/ {$!N;d;}' ".git/config"
# change hardcoded https:// urls of submodules to relative ones
sed -i 's/https:\/\/github.com\/ProjName/../g' ".gitmodules"
# also make the same change in the .git/modules/*/config
sed -i 's/https:\/\/github.com\/ProjName/../g' .git/modules/*/config
# sync and update
git submodule sync
git submodule update --init --recursive --remote

使用上面的 sn-p,它可以满足我的需求。然而,烦人的是,.git/modules/ 文件夹似乎不受版本控制,但如果我只是删除它,git submodule sync 和大多数其他 Git 操作就会停止工作。

有没有办法在修改.gitmodules.git/config 文件后重新生成.git/modules

【问题讨论】:

    标签: git git-submodules


    【解决方案1】:

    如果您只想修改用于本地 repo 的子模块的 URL,则不要修改 .gitmodules 文件,这仅适用于您要推送的更改。

    相反,首先初始化本地子模块配置:

    git submodule init
    

    然后像往常一样修改.git/config文件以更改子模块URL。 (同样,这里不需要修改.gitmodules,如果这是一个新的克隆,你可能还没有.git/modules。)

    作为@jthill points out,修改子模块 URL 的更简单方法是:

    git config submodule.moduleName.url ssh://user@server/path
    

    此时您不想想要运行git submodule sync,因为这会覆盖您刚刚使用.gitmodules 文件中的值所做的更改。相反,直接去:

    git submodule update --recursive --remote
    

    【讨论】:

    • 是的,这回答了我原来的问题,非常感谢。作为一个稍微不同的问题,如果.git/modules 文件夹被弄乱了,有没有办法重新创建它?因为,一旦它被创建,如果我手动删除它,几乎所有的 git 命令从那时起都会被打乱。
    • @abdus_salam 可能的解决方案here
    • 查看git submodule init 文档,它清楚地解释了它的工作原理和原因。
    • 这种方法的一个缺点是,如果设置了submodule.<name>.branchgit submodule update --remote 可能会有其他副作用。我建议使用git submodule set-url,后跟git restore .gitmodules
    【解决方案2】:

    我尝试了上述解决方案,但由于某种原因遇到了一堆问题。以下是替代方案。

    在新的存储库中:

    1. git submodule init
    2. 使用git clone ../local/path/to/submodule.git path/in/repo/submodule手动初始化每个子模块

    每当根存储库子模块哈希更改并且您需要同步子模块时:

    1. 更新根存储库
    2. git submodule update 可能有 --force--recursive 很好的衡量标准

    【讨论】:

      【解决方案3】:

      您不仅可以使用git config submodule.moduleName.url 更改子模块URL,而且:

      • 使用 Git 2.25(2020 年第一季度),您可以修改它。
        请参阅“Git submodule url changed”和新命令git submodule set-url [--] <path> <newurl>

      • 使用 Git 2.22(2019 年第二季度),您还可以取消设置

      commit b57e811commit c89c494(2019 年 2 月 8 日)和 commit 7a4bb55(2019 年 2 月 7 日)Denton Liu (Denton-L)
      (由 Junio C Hamano -- gitster -- 合并,commit 01f8d78,4 月 25 日2019)

      submodule--helper:教配置子命令--unset

      这会教submodule--helper config --unset 选项,它删除 .gitmodule 文件中指定的配置密钥。

      即:

      git submodule--helper config --unset submodule.submodule.url &&
      git submodule--helper config submodule.submodule.url >actual &&
      test_must_be_empty actual
      

      在 Git 2.27(2020 年第二季度)中,继续用 C 重写“git submodule”的各个部分,包括 set-url

      参见Shourya Shukla (periperidip)commit 6417cf9(2020 年 5 月 8 日)。
      (由 Junio C Hamano -- gitster -- 合并到 commit 9e8ed17,2020 年 5 月 13 日)

      submodule: 端口子命令 'set-url' 从 shell 到 C

      签字人:Shourya Shukla

      将子模块子命令 'set-url' 转换为内置命令。端口 'set-url' 到 'submodule--helper.c' 并通过 'git submodule.sh' 调用后者。

      【讨论】:

        【解决方案4】:

        我相信这种方法比公认的答案更有优势:

        $ git submodule set-url moduleName ssh://user@server/path
        $ git restore .gitmodules  # or: git reset HEAD .gitmodules
        

        这将在任何地方更改 URL,然后在您不想更改的地方 (.gitmodules) 将其设置回之前的值。

        为什么这比公认的答案更好:当使用git config submodule.moduleName.url 设置 URL 时,用于将此更改传播到实际子模块检出的命令是 git submodule update --remote。但是,更新 URL 只是该命令的副作用,它的主要用途是(可能)检查子模块中的不同提交。另一方面,set-url 只会按照罐头上所说的去做。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-01-23
          • 1970-01-01
          • 2012-12-20
          • 1970-01-01
          • 2023-01-19
          • 2019-08-09
          • 2014-02-17
          • 2012-06-07
          相关资源
          最近更新 更多