2014-12-03 162 views
0

基本上,我有以下文件夾結構:的.gitignore不能忽略的所有文件夾除外

repo 
    README.md 
    testing_folder 
      readingme.md 
    resources_folder 
.gitignore file

而且,我想忽略除當前目錄下的文件夾的所有文件,這是什麼我有:

* 
!.gitignore 
!*/ 

當我做git status,這就是我得到:

On branch master 
Your branch is ahead of 'origin/master' by 1 commit. 
    (use "git push" to publish your local commits) 
Changes to be committed: 
    (use "git reset HEAD <file>..." to unstage) 

    modified: .gitignore 

Changes not staged for commit: 
    (use "git add <file>..." to update what will be committed) 
    (use "git checkout -- <file>..." to discard changes in working directory) 

    modified: .gitignore 
    modified: README.md 

問題是:它沒有顯示testing_folderresources_folderuntracked。當我嘗試做git add testing/readingme.md,我得到了以下錯誤消息:

The following paths are ignored by one of your .gitignore files: 
testing/readingme.md 
Use -f if you really want to add them. 
fatal: no files added 

我開始變得很困惑,因爲我已經看了這麼多其他相關的職位。他們似乎工作,但我的工作不起作用。

我試圖在.gitignore文件中包括和排除!*/語句,但仍然不顯示文件夾爲untracked

+0

兒童目錄中的文件呢?你是否希望它們被忽略,或只有頂級目錄中的文件? – 2014-12-03 20:11:53

+0

涵蓋兩種情況的答案都很好。 – mynameisJEFF 2014-12-03 20:15:19

回答

2

我已經重新創建你在後顯示的目錄結構,而當我.gitignore包含:

/* 
!*/                   
!.gitignore 

我也注意到,在第一線爲*,而不是/*我有同樣的問題,因爲你。我猜想,添加/可以讓git有足夠的上下文來做你想做的事情。隨着/*git status回報:

On branch master 
Changes to be committed: 
    (use "git reset HEAD <file>..." to unstage) 

    modified: .gitignore 

Untracked files: 
    (use "git add <file>..." to include in what will be committed) 

    testing_folder/ 

這種意志,實際上,只跟蹤其不在父目錄中的文件。 resources_folder未顯示的原因是因爲git不跟蹤空目錄。 There are some ways to trick git into tracking empty folders,但它不是默認行爲。

+0

我現在有點困惑。那麼'/ *'和'*'之間的主要區別是什麼?何時使用哪個? – mynameisJEFF 2014-12-03 23:04:03

+0

說實話,我真的不確定爲什麼'/ *'在'*'不行的時候有效。我認爲一般情況下,如果你使用'*',git將非常自由地忽略事情,因爲你告訴它只是繼續前進而忽視所有事情。使用'/ *'雖然看起來很相似,但明確地不忽略根,這可能會讓你對被忽略的東西和包含的東西更加嚴格。 – 2014-12-04 14:37:34

相關問題