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

时间:2012-04-19 00:45:19

标签: git git-submodules

解决方案:从--cached移除git rm -r --cached submodule/nameScripted供参考。


我正在尝试删除基于this SO answer的git子模块,但子模块未被删除。

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

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

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

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

$> git submodule add git@github.com:lift-stack/lift_24_sbt.git submodules/lift_24_sbt
Adding submodule from repo git@github.com: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

2 个答案:

答案 0 :(得分:12)

你所看到的是正确的; git rm --cached -r确实,实际上只从working tree中删除index中的文件。如果您希望gitindex working tree删除文件,则不应使用--cached。有关详细信息,请参阅git-rm man page


以下是您所做的解释。我假设你输入了你所采取的步骤,而不是从终端复制;据我所知,git add -anot a known git-add flag;我也很确定你的意思是git commit -m <message>

您采取的减少步骤:


# First, add the submodule. 
$> git submodule add git@github.com: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'

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

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

注意:在这里,我们 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.

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

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

如您所见,子模块已被删除,一切都很好。但请注意,文件仍然存在于工作树中 - 您仍然可以使用ls查看它们。 :)

答案 1 :(得分:1)

您可以尝试使用新命令(git1.8.3,2013年4月22日)简化脚本,详细解释为“How do I remove a Git submodule?”的新答案:

git submodule deinit

它应该删除子模块工作树,并从.git/config取消注册。