Git rm -r --cached does not delete folder and submodule content

Solution: remove --cached from git rm -r --cached submodule/name . The script is for reference.


I am trying to remove the git submodule based on this SO response , but the submodule is not removed.

I add a submodule, commit the changes, and then delete it with git rm -r --cached $path/to/submodule (minus the final /), committing the changes, but the submodule still exists.

I can use rm -rf submodules/lift_sbt_24 to delete the folder and contents, but why doesn't git rm -r --cached do git rm -r --cached ?

(deleting the corresponding section from .gitmodules works fine, not a problem, therefore not mentioned here)

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

 $> 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 
+7
source share
2 answers

What you see is right; git rm --cached -r does not actually delete files from the working tree , only from index . If you want git delete files from index and working tree , you should not use --cached . See the git-rm page for more information.


The following is an explanation of what you have done. I assume that you typed the steps you took, and do not copy from the terminal; as far as I know, git add -a not a well-known flag git-add ; I'm also sure that you also meant git commit -m <message> .

Cut steps:

 # 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' 

At this point, you have successfully added the module, and everything works as expected.
What you are trying to do next is to remove the module:

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

Note : here do not delete files from working index , only from index , due to --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. 

Then check that we 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 is removed, and everything is fine. Note, however, that the files still exist in the working tree - you can still view them with ls . :)

+12
source

You can try and simplify your script using the new command (git1.8.3, April 22, 2013), described in detail in the new answer to " How to remove a Git submodule? ":

 git submodule deinit 

It should remove the submodule's working tree and also undo it from .git/config .

+1
source

All Articles