2012-04-19 4 views
7

Giải pháp: xóa --cached từ git rm -r --cached submodule/name. Scripted để tham khảo.git rm -r --cách không loại bỏ thư mục con và nội dung


Tôi đang cố xóa một mô-đun con git dựa trên this SO answer, nhưng mô-đun con không bị xóa.

Tôi thêm mô-đun con, cam kết các thay đổi, sau đó xóa nó bằng cách sử dụng git rm -r --cached $path/to/submodule (trừ dấu sau /), cam kết các thay đổi, nhưng mô-đun con vẫn ở đó.

Tôi có thể sử dụng rm -rf submodules/lift_sbt_24 để xóa thư mục và nội dung, nhưng tại sao không phải là git rm -r --cached làm điều đó?

(xóa các phần có liên quan từ .gitmodules hoạt động tốt, không có vấn đề, do đó không đề cập ở đây)

Đây là git 1.7.5.4 trên Ubuntu 11.10, fwiw. Ví dụ hoàn chỉnh:

$> 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 

Trả lời

12

Những gì bạn thấy là chính xác; git rm --cached -r thực hiện không phải, trên thực tế, hãy xóa các tệp khỏi số working tree, chỉ từ số index. Nếu bạn muốn git để xóa các tệp khỏi cả hai số index số working tree, bạn không nên sử dụng --cached. Xem git-rm man page để biết thêm thông tin.


Sau đây là giải thích về những gì bạn đã làm. Tôi giả định rằng bạn đã gõ các bước bạn đã thực hiện, thay vì sao chép từ một thiết bị đầu cuối; theo như tôi biết, git add -anot a known git-add flag; Tôi cũng khá chắc chắn bạn cũng có nghĩa là git commit -m <message>.

Các bước cắt xuống bạn đã thực hiện:


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

Tại thời điểm này, bạn đã thêm thành công mô-đun, và tất cả mọi thứ đang làm việc như mong đợi.
gì bạn cố gắng làm tiếp theo là để loại bỏ module:

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

Note: ở đây, chúng ta làm không loại bỏ các tập tin từ working index, chỉ từ index, vì các --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. 

Sau đó, kiểm tra xem chúng tôi đã xoá submodule:

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

Như bạn có thể thấy, submodule đã bị xóa, và mọi thứ đều tốt. Tuy nhiên, lưu ý rằng các tệp vẫn tồn tại trong cây làm việc - bạn vẫn có thể xem chúng với ls.:)

+0

Cảm ơn và có nghĩa là 'git add -A' và' git commit -m'. Đã xóa - được tách khỏi 'git rm -r $ 1' và [kịch bản lệnh] (https://github.com/byrongibson/scripts/blob/master/git-rm-submodule.sh). Có lẽ không khôn ngoan để làm điều này trong một kịch bản, nhưng nó ít hữu ích như là một tài liệu tham khảo từng bước một. – Kurtosis

1

Bạn có thể thử và simpliyfy kịch bản của bạn, bằng cách sử dụng một lệnh mới (git1.8.3, tháng Tư Đoàn 22, 2013), chi tiết trong một câu trả lời mới để "How do I remove a Git submodule?":

git submodule deinit 

Nó nên loại bỏ cây làm việc submodule cũng như unregister nó từ .git/config.