簡體   English   中英

如何從主 git 存儲庫中刪除未跟蹤的目錄樹(子模塊/子存儲庫)?

[英]How do I remove untracked directory tree (submodule / sub-repository) from main git repository?

對於我的一生,我似乎無法從主 git 存儲庫中“刪除”未跟蹤的子模塊/目錄。 我嘗試了各種語法,但我不斷收到“pathspec”錯誤,指出不匹配,即使該文件夾位於普通站點中。 我將它添加到.gitignore並且它還沒有被添加或提交到主存儲庫,但它在未跟蹤的Untracked files:當我運行git status時,我無法擺脫它。 git clean -n也沒有顯示任何內容。

關於我在這里做錯了什么的任何線索?

明確一點:不想刪除子倉庫,我只想完全單獨跟蹤子倉庫,並讓主倉庫完全忽略它,就好像它不存在一樣。

### /home/user/  <-- main/master repository (ACTUAL), separate git-directory

$pwd
/home/user


$cat .git
gitdir: /home/git-repositories/user-main


### /home/user/ntc-templates/   <-- sub repository, separate git-directory

$cat ntc-templates/.git
gitdir: /home/git-repositories/ntc-templates


$git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   .recently-used

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    ntc-templates/     ### <<------------------ how do I get rid of this line?

no changes added to commit (use "git add" and/or "git commit -a")


$git rm -rf ntc-templates/
fatal: pathspec 'ntc-templates/' did not match any files


$git rm -rfn --cached ntc-templates/
fatal: pathspec 'ntc-templates/' did not match any files


$git rm -rfn --cached ntc-templates
fatal: pathspec 'ntc-templates' did not match any files


$git rm -rfn --cached ~/ntc-templates
fatal: pathspec '/home/user/ntc-templates' did not match any files


$ll ntc-templates/
total 168
drwxr-xr-x  6 user user  4096 Feb 10 11:49 ./
drwxr-xr-x 49 user user  4096 Feb 10 11:59 ../
-rw-r--r--  1 user user 29276 Feb 10 11:48 CHANGELOG
-rw-r--r--  1 user user 15443 Feb 10 11:48 development_scripts.py
-rw-r--r--  1 root root    45 Feb 10 11:49 .git
drwxr-xr-x  2 user user  4096 Feb 10 11:48 .github/
-rw-r--r--  1 user user    54 Feb 10 11:48 .github_changelog_generator
-rw-r--r--  1 user user  2622 Feb 10 11:48 .gitignore
drwxr-xr-x  3 user user  4096 Feb 10 11:48 lib/
-rw-r--r--  1 user user   601 Feb 10 11:48 LICENSE
-rw-r--r--  1 user user    29 Feb 10 11:48 MANIFEST.in
-rw-r--r--  1 user user   232 Feb 10 11:48 pyproject.toml
-rw-r--r--  1 user user 22240 Feb 10 11:48 README.md
-rw-r--r--  1 user user  1412 Feb 10 11:48 setup.py
drwxr-xr-x  2 user user 36864 Feb 10 11:48 templates/
drwxr-xr-x 37 user user  4096 Feb 10 11:48 tests/
-rw-r--r--  1 user user   243 Feb 10 11:48 tox.ini
-rw-r--r--  1 user user   101 Feb 10 11:48 .travis.yml
-rw-r--r--  1 user user   703 Feb 10 11:48 .yamllint


$git-ignore | grep ntc
ntc-templates/**
ntc-templates/CHANGELOG
ntc-templates/development_scripts.py
ntc-templates/lib
ntc-templates/LICENSE
ntc-templates/MANIFEST.in
ntc-templates/pyproject.toml
ntc-templates/README.md
ntc-templates/setup.py
ntc-templates/templates
ntc-templates/tests
ntc-templates/tox.ini


$

謝謝。

未跟蹤的文件是磁盤上的文件/目錄,git 尚未跟蹤,也沒有明確忽略。

您可以告訴 git 忽略它,或者從磁盤中刪除該文件/目錄:

# option 1 : ignore that directory
$ echo "nc-templates" >> .gitignore
$ git add .gitignore
$ git commit

# I think this is *not* what you want, but for completeness :
# option 2 : remove the directory
$ rm -rf nc-templates

注意:實際上有幾種方法可以忽略文件/目錄,例如:

你什么時候會使用 .git/info/exclude 而不是 .gitignore 來排除文件?


[編輯]
為什么.gitignorentc-templates/** “不起作用”:

ntc-templates/本身就是一個 git 存儲庫,因此它被視為潛在的子模塊。 父 git 存儲庫不會查看ntc-templates/下的文件(它們將由子存儲庫本身處理),因此在ntc-templates/下挖掘的模式(如ntc-templates/** )不適用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM