How do I remove a submodule?

2018-12-31 02:41发布

How do I remove a Git submodule?

By the way, is there a reason I can't simply do git submodule rm whatever ?

21条回答
何处买醉
2楼-- · 2018-12-31 02:47

If the submodule was accidentally added because you added, committed and pushed a folder that was already a Git repository (contained .git), you won’t have a .gitmodules file to edit, or anything in .git/config. In this case all you need is :

git rm --cached subfolder
git add subfolder
git commit -m "Enter message here"
git push

FWIW, I also removed the .git folder before doing the git add.

查看更多
ら面具成の殇う
3楼-- · 2018-12-31 02:48

Via the page Git Submodule Tutorial:

To remove a submodule you need to:

  1. Delete the relevant section from the .gitmodules file.
  2. Stage the .gitmodules changes git add .gitmodules
  3. Delete the relevant section from .git/config.
  4. Run git rm --cached path_to_submodule (no trailing slash).
  5. Run rm -rf .git/modules/path_to_submodule
  6. Commit git commit -m "Removed submodule <name>"
  7. Delete the now untracked submodule files
    rm -rf path_to_submodule

See also: alternative steps below.

查看更多
十年一品温如言
4楼-- · 2018-12-31 02:49

The majority of answers to this question are outdated, incomplete, or unnecessarily complex.

A submodule cloned using git 1.7.8 or newer will leave at most four traces of itself in your local repo. The process for removing those four traces is given by the three commands below:

# Remove the submodule entry from .git/config
git submodule deinit -f path/to/submodule

# Remove the submodule directory from the superproject's .git/modules directory
rm -rf .git/modules/path/to/submodule

# Remove the entry in .gitmodules and remove the submodule directory located at path/to/submodule
git rm -f path/to/submodule
查看更多
听够珍惜
5楼-- · 2018-12-31 02:53

To summarize, this is what you should do :

  1. Set path_to_submodule var (no trailing slash):

    path_to_submodule=path/to/submodule

  2. Delete the relevant line from the .gitmodules file:

    git config -f .gitmodules --remove-section submodule.$path_to_submodule

  3. Delete the relevant section from .git/config

    git config -f .git/config --remove-section submodule.$path_to_submodule

  4. Unstage and remove $path_to_submodule only from the index (to prevent losing information)

    git rm --cached $path_to_submodule

  5. Track changes made to .gitmodules

    git add .gitmodules

  6. Commit the superproject

    git commit -m "Remove submodule submodule_name"

  7. Delete the now untracked submodule files

    rm -rf $path_to_submodule

    rm -rf .git/modules/$path_to_submodule

查看更多
明月照影归
6楼-- · 2018-12-31 02:54

Here are the 4 steps that I found necessary or useful (important ones first):

git rm -f the_submodule
rm -rf .git/modules/the_submodule
git config -f .git/config --remove-section submodule.the_submodule
git commit -m "..."

In theory, git rm in step 1 should take care of it. Hopefully, the second part of OP question can be answered positively one day (that this can be done in one command).

But as of July 2017, step 2 is necessary to remove data in .git/modules/ for otherwise, you can't e.g. add the submodule back in the future.

You can probably get away with the above two steps for git 1.8.5+ as tinlyx's answer noted, as all git submodule commands seem to work.

Step 3 removes the section for the_submodule in the file .git/config. This should be done for completeness. (The entry may cause problems for older git versions, but I don't have one to test).

For this, most answers suggest using git submodule deinit. I find it more explicit and less confusing to use git config -f .git/config --remove-section. According to the git-submodule documentation, git deinit:

Unregister the given submodules ... If you really want to remove a submodule from the repository and commit that use git-rm[1] instead.

Last but not least, if you don't git commit, you will/may get an error when doing git submodule summary (as of git 2.7):

fatal: Not a git repository: 'the_submodule/.git'
* the_submodule 73f0d1d...0000000:

This is regardless of whether you do steps 2 or 3.

查看更多
美炸的是我
7楼-- · 2018-12-31 02:57

You can use an alias to automate the solutions provided by others:

[alias]
  rms = "!f(){ git rm --cached \"$1\";rm -r \"$1\";git config -f .gitmodules --remove-section \"submodule.$1\";git config -f .git/config --remove-section \"submodule.$1\";git add .gitmodules; }; f"

Put that in your git config, and then you can do: git rms path/to/submodule

查看更多
登录 后发表回答