如何在git版本1.9.3中移除git子模块?

103
截至git版本1.9.3(Apple Git-50)在mac上如何删除git子模块?我看到很多过时的信息,许多开发人员告诉我它们不起作用。现行方法是什么? git deinit pathToSubModule是否有效?
我原以为以下步骤会起作用,链接在这里,但评论说它们不会起作用。
让我解释一下我的当前情况和我需要完成的任务。我已经安装了Quick存储库并将其作为子模块添加到我的项目中。这段代码已经被检入并且其他人正在使用它。现在我需要做的是在更安全的公司github上对同一个Quick存储库进行分叉(fork),然后将其作为git子模块添加,并让它替换之前安装的当前Quick子模块。
更新:我读到最新的git版本的正确方式是什么,请确认?
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

我为子模块问题苦斗了两天。突破口在于我找到了这个链接:https://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(查找您服务器上的实际路径),所有奇怪的问题都在十分钟内消失了。 - Elise van Looij
3个回答

220
你有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

注销给定的子模块,即从.git/config移除整个submodule.$name部分以及它们的工作目录。

后续对git submodule updategit submodule foreachgit submodule sync的调用将跳过任何未注册的子模块,直到它们再次初始化,因此如果您不想在工作目录中拥有子模块的本地检出,请使用此命令。

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

如果指定了--force,则即使子模块的工作目录包含本地修改,也将删除该子模块的工作目录。


9
我希望您能帮我彻底删除子模块,包括其他用户的子模块。所以我只需执行以下命令:git submodule deinit asubmodule; 和 git rm asubmodule ?? 另外,其他用户缓存的子模块会怎样处理?他们在拉取后需要将其删除吗? - j2emanue
1
我想删除子模块的本地副本,这个方法完美解决了问题,子目录中的所有文件都被删除了。 - malhal
1
如果您已经检出了一个取消注册子模块的基本存储库版本,则可能还需要使用 rm -rf <asubmodule> - Sarah Messer

15

我正在使用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 等命令撤销其删除。


2
最近的答案和最短的解决方案 - TurboGuma

4

如何安全地移除子模块。

(补充https://dev59.com/QHM_5IYBdhLWcg3wt1k0#1260982

  1. List and locate the submodule section in .gitmodules file. Say via terminal vi .gitmodules. Example:

    [submodule "submodules/afnetworking"]
        path = submodules/afnetworking
        url = https://github.com/CompanyName/afnetworking.git
    
  2. Submodules can be used with local forks. For a long running projects, code may differ from original repo and might have some fixes. It's a good idea to verify if submodule code went through changes over the course of project. Example: Look at the logs, if it's pointing to master branch or some local branch. exploring git logs to find modifications that were done via terminal. typically these are stored under a directory, my example, under submodules directory.

    User$ cd submodules/afnetworking
    User$ git log
    
  3. Remove the submodule section from .gitmodules, save the file. Example: git status should show only following as modified

    modified:   .gitmodules
    
  4. Stage the changes with git add .gitmodules.

  5. List and locate the submodule section in .git/config files. Say via terminal vi .git/config. Example:

    [submodule "submodules/afnetworking"]
        url = https://github.com/CompanyName/afnetworking.git
    
  6. Remove the git cache sobmodule files. running git rm --cached submodules/afnetworking (no trailing slash) would remove it successfully. Example:

    User$ git rm --cached submodules/afnetworking
    rm 'submodules/afnetworking'    <-- terminal output
    
  7. Remove the submodule files under .git directory with rm -rf .git/modules/.... Confirm it with git status afterwards.

    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. Commit the changes. Confirm with git status afterwards.

    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. Delete the untracked submodule files.

    User$ rm -rf submodules/afnetworking
    
  10. Delete the references from Xcode project. Build, fix compile and runtime issues.


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接