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

Kur*_*sis 7 git git-submodules

解决方案:--cached从中删除git rm -r --cached submodule/name. 脚本供参考.


我正在尝试根据此SO答案删除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
Run Code Online (Sandbox Code Playgroud)

sim*_*ont 12

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


以下是您所做的解释.我假设你输入了你所采取的步骤,而不是从终端复制; 据我所知,git add -a没有一个已知的混帐添加标志 ; 我也很确定你的意思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'
Run Code Online (Sandbox Code Playgroud)

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

$> git rm -r --cached submodules/lift_24_sbt
Run Code Online (Sandbox Code Playgroud)

:在这里,我们也不会从删除的文件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.
Run Code Online (Sandbox Code Playgroud)

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

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

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