【问题标题】:What is the way to remove a git submodule as of git version 1.9.3?从 git 版本 1.9.3 开始删除 git 子模块的方法是什么?
【发布时间】:2015-04-24 14:24:31
【问题描述】:

从 Mac 上的 git 版本 1.9.3 (Apple Git-50) 开始,我如何删除 git 子模块?我正在阅读很多过时的信息,许多开发人员告诉我他们不会工作。目前的方式是什么? git deinit pathToSubModule 会成功吗?

我认为可行的步骤是here,但 cmets 说他们不会。

让我解释一下我目前的情况以及我需要完成的工作。我已经安装了the Quick repository 并将其作为子模块添加到我的项目中。此代码已签入,其他人正在使用它。我现在需要做的是 fork 相同的快速存储库并将其托管在我公司拥有的更安全的 github 上(因此完全是其他私有 github)。分叉后,我想将该分叉添加为 gitSubmodule 并让它替换我之前安装的当前 Quick 子模块。

更新:我读到以下是最新版本的正确方法请确认?

To remove a submodule added using:

git submodule add blah@blah.com:repos/blah.git lib/blah
Run:

git rm lib/blah
That's it.

For old versions of git (circa ~1.8.5) use:

git submodule deinit lib/blah
git rm lib/blah
git config -f .gitmodules --remove-section submodule.lib/blah

【问题讨论】:

  • 我已经解决了两天的子模块问题。当我发现这个时,突破就来了:forums.developer.apple.com/thread/13102。基本上,Xcode,也许还有其他应用程序,都在努力扩展包含“~”的 url。一旦我将 ssh://username@server.remoteHost.com/~/git/MyRepo.git 更改为 ssh://username@server.remoteHost.com/home/username/git/MyRepo.git (查找实际路径在您的服务器上),十分钟后所有的怪异消失了。

标签: git github git-submodules


【解决方案1】:

你有git submodule deinit

git submodule deinit <asubmodule>    
git rm <asubmodule>
# Note: asubmodule (no trailing slash)
# or, if you want to leave it in your working tree
git rm --cached <asubmodule>
rm -rf .git/modules/<asubmodule>

deinit

取消注册给定的子模块,即删除整个submodule.$name
.git/config 的部分以及他们的工作树。

git submodule updategit submodule foreachgit submodule sync 的进一步调用将跳过任何未注册的子模块,直到它们再次被初始化,因此如果您不想进行本地结帐,请使用此命令不再是工作树中的子模块。

如果您真的想从存储库中删除子模块并提交,请改用git rm

如果指定--force,子模块的工作树即使包含本地修改也会被移除。

【讨论】:

  • 我想彻底清除我和其他用户的子模块。所以我只需要 git submodule deinit asubmodule;和 git rm asubmodule ??还有其他用户缓存的子模块呢,拉下来后要不要删除?
  • 我想删除子模块的本地副本,这很完美,子目录中的所有文件都消失了。
  • 如果您已经签出了已取消注册子模块的基本存储库版本,您可能还需要rm -rf &lt;asubmodule&gt;
【解决方案2】:

我使用的是 Git 2.16.2 版,git rm 做得很好:

git rm path-to-submodule

您可以使用git statusgit diff --cached 验证这会取消初始化子模块并自动修改.gitmodules。与往常一样,您需要提交更改。

但是,即使子模块已从源代码管理中删除,.git/modules/path-to-submodule 仍包含子模块存储库,.git/config 包含其 URL,因此您仍需要手动删除它们:

git config --remove-section submodule.path-to-submodule
rm -rf .git/modules/path-to-submodule

保留子模块存储库和配置是有意的,以便您可以撤消删除,例如git reset --hard.

【讨论】:

  • 最新答案和最短解决方案
【解决方案3】:

如何安全地移除子模块。

(添加到https://stackoverflow.com/a/1260982/342794

  1. 列出并定位.gitmodules 文件中的子模块部分。通过终端 vi .gitmodules 说。示例:

    [submodule "submodules/afnetworking"]
        path = submodules/afnetworking
        url = https://github.com/CompanyName/afnetworking.git
    
  2. 子模块可以与本地分叉一起使用。对于长期运行的项目,代码可能与原始 repo 不同,并且可能有一些修复。验证子模块代码是否在项目过程中经历了更改是一个好主意。示例:查看日志,如果它指向 master 分支或某个本地分支。探索 git 日志以查找通过终端完成的修改。通常这些存储在一个目录下,我的示例是在submodules 目录下。

    User$ cd submodules/afnetworking
    User$ git log
    
  3. 从 .gitmodules 中删除子模块部分,保存文件。示例:git status 应仅显示以下已修改

    modified:   .gitmodules
    
  4. 使用git add .gitmodules 暂存更改。

  5. 列出并定位.git/config 文件中的子模块部分。通过终端 vi .git/config 说。示例:

    [submodule "submodules/afnetworking"]
        url = https://github.com/CompanyName/afnetworking.git
    
  6. 删除 git 缓存 sobmodule 文件。运行git rm --cached submodules/afnetworking(没有尾部斜杠)将成功删除它。示例:

    User$ git rm --cached submodules/afnetworking
    rm 'submodules/afnetworking'    <-- terminal output
    
  7. rm -rf .git/modules/... 删除.git 目录下的子模块文件。之后通过git status 确认。

    User$ rm -rf .git/modules/submodules/afnetworking/
    User$ git status
    On branch feature/replace-afnetworking-submodule-with-pod
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
        modified:   .gitmodules
        deleted:    submodules/afnetworking
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
        submodules/afnetworking/
    
  8. 提交更改。之后与git status 确认。

    User$ git commit -m "Remove submodule afnetworking"
    [feature/replace-afnetworking-submodule-with-pod 70e239222] Remove submodule afnetworking
    2 files changed, 4 deletions(-)
      delete mode 160000 submodules/afnetworking
    User$ git status
    On branch feature/replace-afnetworking-submodule-with-pod
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
        submodules/afnetworking/
    nothing added to commit but untracked files present (use "git add" to track)
    
  9. 删除未跟踪的子模块文件。

    User$ rm -rf submodules/afnetworking
    
  10. 从 Xcode 项目中删除引用。构建、修复编译和运行时问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-03
    • 2021-04-27
    • 1970-01-01
    • 2014-02-18
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 2018-07-04
    相关资源
    最近更新 更多