git rm -r --cached 不删除子模块文件夹和内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10220140/
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-19 06:47:29  来源:igfitidea点击:

git rm -r --cached not removing submodule folder and contents

gitgit-submodules

提问by Kurtosis

Solution: remove --cachedfrom git rm -r --cached submodule/name. Scriptedfor reference.

解决方案:--cachedgit rm -r --cached submodule/name. 编写脚本以供参考。



I'm trying to remove a git submodule based on this SO answer, but the submodule is not being removed.

我正在尝试根据这个 SO answer删除一个 git 子模块,但该子模块没有被删除。

I add the submodule, commit the changes, then delete it using git rm -r --cached $path/to/submodule(minus the trailing / ), commit the changes, but the submodule is still there.

我添加子模块,提交更改,然后使用git rm -r --cached $path/to/submodule(减去尾随 / )删除它,提交更改,但子模块仍然存在。

I can use rm -rf submodules/lift_sbt_24to delete the folder and contents, but why isn't git rm -r --cacheddoing that?

我可以rm -rf submodules/lift_sbt_24用来删除文件夹和内容,但为什么不git rm -r --cached这样做呢?

(deleting the relevant section from .gitmodules works fine, is no problem, hence not mentioned here)

(从 .gitmodules 中删除相关部分工作正常,没问题,因此这里没有提到)

This is git 1.7.5.4 on Ubuntu 11.10, fwiw. Complete example:

这是 Ubuntu 11.10 上的 git 1.7.5.4,fwiw。完整示例:

$> git submodule add [email protected]:lift-stack/lift_24_sbt.git submodules/lift_24_sbt
Adding submodule from repo [email protected]:lift-stack/lift_24_sbt.git as submodules/lift_24_sbt
Cloning into submodules/lift_24_sbt...
remote: Counting objects: 619, done.
remote: Compressing objects: 100% (375/375), done.
remote: Total 619 (delta 172), reused 593 (delta 147)
Receiving objects: 100% (619/619), 1.74 MiB | 112 KiB/s, done.
Resolving deltas: 100% (172/172), done.
$> git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commits.
#
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   .gitmodules
#   new file:   submodules/lift_24_sbt
#
$> git add -a
$> git commit 'added submodules/lift_24_sbt'
[master 9894113] update
 2 files changed, 4 insertions(+), 0 deletions(-)
 create mode 160000 submodules/lift_24_sbt
$> git rm -r --cached submodules/lift_24_sbt
rm 'submodules/lift_24_sbt'
$> git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commits.
#
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   deleted:    submodules/lift_24_sbt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   submodules/lift_24_sbt/
$> git add -a
$> git commit -m 'deleted submodules/lift_24_sbt'
# On branch master
# Your branch is ahead of 'origin/master' by 1 commits.
#
nothing to commit (working directory clean)
$> ls -al submodules/lift_24_sbt/
total 1060
drwxr-xr-x 5 kurtosis kurtosis    4096 2012-04-18 17:26 ./
drwxrwxr-x 6 kurtosis kurtosis    4096 2012-04-18 17:26 ../
drwxrwxr-x 8 kurtosis kurtosis    4096 2012-04-18 17:32 .git/
drwxrwxr-x 2 kurtosis kurtosis    4096 2012-04-18 17:26 project/
drwxrwxr-x 3 kurtosis kurtosis    4096 2012-04-18 17:26 src/
-rw-rw-r-- 1 kurtosis kurtosis     931 2012-04-18 17:26 build.sbt
-rw-rw-r-- 1 kurtosis kurtosis     463 2012-04-18 17:26 .gitignore
-rw-rw-r-- 1 kurtosis kurtosis      91 2012-04-18 17:26 README.md
-rwxrwxr-x 1 kurtosis kurtosis     110 2012-04-18 17:26 sbt*
-rw-rw-r-- 1 kurtosis kurtosis     131 2012-04-18 17:26 sbt.bat
-rw-rw-r-- 1 kurtosis kurtosis 1041753 2012-04-18 17:26 sbt-launch.jar
$> git --version
git version 1.7.5.4

回答by simont

What you're seeing is correct; git rm --cached -rdoes not, in fact, remove the files from the working tree, only from the index. If you want gitto remove the files from both the indexandthe working tree, you shouldn't use --cached. See the git-rmman pagefor further information.

你所看到的是正确的;git rm --cached -r,其实,从删除文件working tree仅从,index。如果你想git从两个删除文件indexworking tree,你不应该使用--cached。有关更多信息,请参阅git-rm手册页



Following is an explanation of what you did. I am assuming that you typed out the steps you took, rather than copying from a terminal; as far as I'm aware, git add -ais not a known git-add flag; I am also fairly sure you also meant git commit -m <message>.

以下是对你所做的事情的解释。我假设您输入了您采取的步骤,而不是从终端复制;据我所知,git add -a没有一个已知的git-附加标志; 我也很确定你也是这个意思git commit -m <message>

The cut-down steps you've taken:

您已采取的缩减步骤:


# First, add the submodule. 
$> git submodule add [email protected]:lift-stack/lift_24_sbt.git submodules/lift_24_sbt
# Check that the submodule exists. (It does). 
$> git status
# Add everything to the staging area from the working tree. 
$> git add -a
# Commit all changes. 
$> git commit 'added submodules/lift_24_sbt'

At this point, you've successfully added the module, and everything is working as expected.
What you try to do next is to removethe module:

此时,您已成功添加模块,一切都按预期工作。
您接下来要做的是删除模块:

$> git rm -r --cached submodules/lift_24_sbt

Note: here, we do notremove the files from the working index, onlyfrom the index, because of the --cached:

注意:在这里,我们不会从 中删除文件working index,而只是从 中删除index,因为--cached

--cached
       Use this option to unstage and remove paths only from the index. Working tree
       files, whether modified or not, will be left alone.
--cached
       Use this option to unstage and remove paths only from the index. Working tree
       files, whether modified or not, will be left alone.

Then, check that we've removed the submodule:

然后,检查我们是否删除了子模块:

$> git status
... <snip> 
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   deleted:    submodules/lift_24_sbt

As you can see, the submodule has been deleted, and everything is good. Note, however, that the files still exist in the working tree- you can still view them with ls. :)

可以看到,子模块已经删除,一切正常。但是请注意,这些文件仍然存在于工作树中- 您仍然可以使用ls. :)

回答by VonC

You can try and simpliyfy your script, by using a new command (git1.8.3, April 22d, 2013), detailed in a new answer to "How do I remove a Git submodule?":

您可以尝试使用新命令(git1.8.3,2013 年 4 月 22 日)简化您的脚本,该命令在“如何删除 Git 子模块?”的新答案中有详细说明:

git submodule deinit

It should remove the submodule working tree as well as unregister it from .git/config.

它应该删除子模块工作树并从.git/config.