2012-05-16 87 views
7

閱讀.gitignore幾個問題後,所有文件夾中的某些文件,我發現沒有一個可以回答我的問題:忽略使用單一的.gitignore文件

我有什麼要添加到的.gitignore忽略所有文件一定的結局,在所有文件夾中說 *.txt

我寧願只在頂層有一個.gitignore文件。

我嘗試了幾件事但沒有工作。

這是我測試的是什麼(.gitignore加入之前,加入任何其他文件的庫):

$ ls 
    abc.txt content 

    $ ls content/ 
    abc.txt def.other def.txt 

    $ echo "" > .gitignore 


    $ git status --ignored --untracked-files=all 
    # On branch master 
    # 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 
    # 
    # Untracked files: 
    # (use "git add <file>..." to include in what will be committed) 
    # 
    #  abc.txt 
    #  content/abc.txt 
    #  content/def.other 
    #  content/def.txt 
    no changes added to commit (use "git add" and/or "git commit -a") 

這是預期:所有的文件顯示,因爲的.gitignore是空的。

$ echo "*.txt" > .gitignore 

    $ git status --ignored --untracked-files=all 
    # On branch master 
    # 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 
    # 
    # Untracked files: 
    # (use "git add <file>..." to include in what will be committed) 
    # 
    #  content/def.other 
    # Ignored files: 
    # (use "git add -f <file>..." to include in what will be committed) 
    # 
    #  abc.txt 
    no changes added to commit (use "git add" and/or "git commit -a") 

爲什麼文件content/abc.txt和content/def.txt不顯示在列表中?

$ git clean -n 
    Would not remove content/ 

我以爲他們會出現在這裏了。

$ echo "" > .gitignore 

    $ git clean -n 
    Would remove abc.txt 
    Would not remove content/ 

    $ cd content 

    $ git clean -n -x 
    Would remove def.other 

    $ git clean -n -x 
    Would remove abc.txt 
    Would remove def.other 
    Would remove def.txt 

如果文件內容/的abc.txt和內容/ def.txt由clean -n -x顯示,但不clean -n,我認爲他們將被忽略。但那爲什麼他們不出現在git status --ignored --untracked-files=all

回答

3

只需加入*.txt即可。檢查gitignore(5) man page爲gitignore格式說明

如果您嘗試添加content目錄,則所有*.txt文件都將被忽略。

$ echo "*.txt" > .gitignore 

$ git add content 

$ git status --ignored --untracked-files=all 
# On branch master 
# 
# Initial commit 
# 
# Changes to be committed: 
# (use "git rm --cached <file>..." to unstage) 
# 
#  new file: content/def.other 
# 
# Untracked files: 
# (use "git add <file>..." to include in what will be committed) 
# 
#  .gitignore 
# Ignored files: 
# (use "git add -f <file>..." to include in what will be committed) 
# 
#  abc.txt 
#  content/abc.txt 
#  content/def.txt 
+1

看來,之所以因爲'git的狀態--ignored --untracked-文件= all'沒有顯示在您的測試這些文件有事情做與「目錄沒有被跟蹤」(實際上,你可以跟蹤目錄,只是文件,但當我跟蹤他們出現的目錄內的文件) – KurzedMetal

+0

這工作。奇怪的是'git clean -fx'只在文件夾包含一個跟蹤文件後刪除內容中的文件。否則,我不得不在文件夾中調用命令來刪除文件! – Onur