删除 git 子模块的当前方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29850029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:07:12  来源:igfitidea点击:

What is the current way to remove a git submodule?

gitgithubgit-submodules

提问by j2emanue

as of git version 1.9.3 (Apple Git-50) on mac how do i remove a git submodule? I am reading alot of outdated information with many developers telling me they wont work. What is the current way ? will git deinit pathToSubModuledo the trick ?

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

The steps i thought would work are herebut comments say they wont.

我认为可行的步骤在这里,但评论说他们不会。

Let me explain my current situation and what i need accomplished. I've installed the Quick repositoryand added it to as submodule to my project. This code is already checked in and others are using it. What i now need to do is forkthe same Quick repository and host it on a more secure github that my company has (so a completely other private github). After forking it i want to add that fork as a gitSubmodule and let it replace the current Quick submodule i had installed previously.

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

update: i've read that the following is the correct way on latest git version please confirm?

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

To remove a submodule added using:

git submodule add [email protected]: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

回答by CodeWizard

You have the git submodule deinit

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

deinit

Un-register the given submodules, i.e. remove the whole submodule.$name
section from .git/configtogether with their work tree.

Further calls to git submodule update, git submodule foreachand git submodule syncwill skipany unregistered submodules until they are initialized again, so use this command if you don't want to have a local checkout of the submodule in your work tree anymore.

If you really want to remove a submodule from the repository and commit that use git rminstead.

If --forceis specified, the submodule's work tree will be removed even if it contains local modifications.

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

进一步呼吁git submodule updategit submodule foreachgit submodule sync跳过所有未注册的子模块,直到它们被再次初始化,如果你不想在你的工作树中的子模块的本地结账了,所以使用这个命令。

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

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

回答by lal

How to safely remove a submodule.

如何安全地移除子模块。

(adds to https://stackoverflow.com/a/1260982/342794)

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

  1. List and locate the submodule section in .gitmodulesfile. 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 masterbranch 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 submodulesdirectory.

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

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

  5. List and locate the submodule section in .git/configfiles. 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 .gitdirectory with rm -rf .git/modules/.... Confirm it with git statusafterwards.

    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 statusafterwards.

    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.

  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. 下删除子模块的文件.git与目录rm -rf .git/modules/...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 项目中删除引用。构建、修复编译和运行时问题。

回答by mrts

I'm using Git version 2.16.2 and git rmdoes the job mostly well:

我正在使用 Git 版本 2.16.2 并且git rm大部分工作做得很好:

git rm path-to-submodule

You can verify with git statusand git diff --cachedthat this deinitializes the submodule and modifies .gitmodulesautomatically. As always, you need to commit the change.

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

However, even though the submodule is removed from source control, .git/modules/path-to-submodulestill contains the submodule repository and .git/configcontains its URL, so you still have to remove those manually:

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

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

Keeping the submodule repository and configuration is intentional so that you can undo the removal with e.g. git reset --hard.

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