Git command to show which specific files are ignored by .gitignore

后端 未结 9 1055
我在风中等你
我在风中等你 2020-11-22 05:07

I am getting my feet wet with Git and have the following issue:

My project source tree:

/
|
+--src/
+----refs/
+----...
|
+--vendor/
+----...
         


        
相关标签:
9条回答
  • 2020-11-22 06:01

    It should be sufficient to use

    git ls-files --others -i --exclude-standard
    

    as that covers everything covered by

    git ls-files --others -i --exclude-from=.git/info/exclude
    

    therefore the latter is redundant.


    You can make this easier by adding an alias to your ~/.gitconfig file:

    git config --global alias.ignored "ls-files --others -i --exclude-standard"
    

    Now you can just type git ignored to see the list. Much easier to remember, and faster to type.

    If you prefer the more succinct display of Jason Geng's solution, you can add an alias for that like this:

    git config --global alias.ignored "status --ignored -s"
    

    However the more verbose output is more useful for troubleshooting problems with your .gitignore files, as it lists every single cotton-pickin' file that is ignored. You would normally pipe the results through grep to see if a file you expect to be ignored is in there, or if a file you don't want to be ignore is in there.

    git ignored | grep some-file-that-isnt-being-ignored-properly
    

    Then, when you just want to see a short display, it's easy enough to remember and type

    git status --ignored
    

    (The -s can normally be left off.)

    0 讨论(0)
  • 2020-11-22 06:05

    Another option that's pretty clean (No pun intended.):

    git clean -ndX
    

    Explanation:

    $ git help clean
    
    git-clean - Remove untracked files from the working tree
    -n, --dry-run - Don't actually remove anything, just show what would be done.
    -d - Remove untracked directories in addition to untracked files.
    -X - Remove only files ignored by Git.
    

    Note: This solution will not show ignored files that have already been removed.

    0 讨论(0)
  • 2020-11-22 06:05

    While generally correct your solution does not work in all circumstances. Assume a repo dir like this:

    # ls **/*                                                                                                       
    doc/index.html  README.txt  tmp/dir0/file0  tmp/file1  tmp/file2
    
    doc:
    index.html
    
    tmp:
    dir0  file1  file2
    
    tmp/dir0:
    file0
    

    and a .gitignore like this:

    # cat .gitignore
    doc
    tmp/*
    

    This ignores the doc directory and all files below tmp. Git works as expected, but the given command for listing the ignored files does not. Lets have a look at what git has to say:

    # git ls-files --others --ignored --exclude-standard                                                            
    tmp/file1
    tmp/file2
    

    Notice that doc is missing from the listing. You can get it with:

    # git ls-files --others --ignored --exclude-standard --directory                                                
    doc/
    

    Notice the additional --directory option.

    From my knowledge there is no one command to list all ignored files at once. But I don't know why tmp/dir0 does not show up at all.

    0 讨论(0)
提交回复
热议问题