2012-04-19 140 views
7

解決方案:從git rm -r --cached submodule/name刪除--cachedScripted僅供參考。git rm -r - 緩存不刪除子模塊文件夾和內容


我試圖刪除基於this SO answer一個git子模塊,但子模塊沒有被刪除。

我添加子模塊,提交更改,然後使用git rm -r --cached $path/to/submodule(減去尾部/)刪除它,提交更改,但子模塊仍然存在。

我可以使用rm -rf submodules/lift_sbt_24刪除文件夾和內容,但爲什麼不是git rm -r --cached這樣做?

(刪除從.gitmodules相關部分工作正常,是沒有問題的,因此這裏不再贅述)

這是混帳1.7.5.4在Ubuntu 11.10,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 

回答

12

你所看到的是正確的; git rm --cached -r確實不是,實際上,從working tree中刪除文件,只能從index中刪除。如果您希望gitindexworking 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 [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' 

在這一點上,你已經成功添加模塊,一切工作正常。
你嘗試下做的就是刪除模塊:

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

注意:在這裏,我們做working indexindex刪除文件,只有,因爲在--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查看它們。:)

+0

謝謝,是的,我的意思是'git add -A'和'git commit -m'。刪除 - 從'git rm -r $ 1'和[腳本化](https://github.com/byrongibson/scripts/blob/master/git-rm-submodule.sh)中緩存。在腳本中這樣做可能不明智,但它至少可以作爲一步一步的參考。 – Kurtosis 2012-05-07 20:17:35

1

你可以嘗試simpliyfy你的腳本,通過使用新的命令(git1.8.3,四月22D,2013),詳細在一個新的答案爲 「How do I remove a Git submodule?」:

git submodule deinit 

應該刪除子模塊工作樹以及從.git/config取消註冊。