在git中显示所有被忽略的文件

Ham*_*amy 5 git gitignore

git ls-files --others --ignored --exclude-standard没有列出一些被忽略的文件的问题.

我的项目有这个目录结构

.
??? aspnet
?   ??? .gitignore
?   ??? __init__.py
?   ??? lib
?   ?   ??? <lots of big stuff>
Run Code Online (Sandbox Code Playgroud)

aspnet/.gitignore名单lib/*,并git add aspnet/lib/foo报告此路径将被忽略.

但是git ls-files --others --ignored --exclude-standard不列出下的文件lib.这些是未跟踪的文件,如果我这样做,它们会显示在输出中git ls-files --others,但如果我提供了忽略的标记则不会显示.

使用git版本1.7.9.5

编辑:使用git版本1.8.5.2(Apple Git-48)按预期工作,这似乎是一个git bug

hek*_*mgl 13

拥有find(可能在UNIX/Linux上),您可以在git存储库的根文件夹中发出以下命令:

find . -type f  | git check-ignore --stdin
Run Code Online (Sandbox Code Playgroud)

find . -type f将以递归方式列出文件夹中的所有文件,同时git check-ignore列出列表中的那些文件,这些文件被有效忽略.gitignore.


check-ignore命令相对较新.如果您的.git版本不支持它,您可以使用以下解决方法与POSIX兼容的shell(如bash,sh,dash,zsh).它基于.gitignore包含由shell解释的glob模式的事实.解决方法迭代遍历glob模式.gitignore,在shell中展开它们并从中过滤掉目录:

while read glob ; do
    if [ -d "$glob" ] ; then
        # Be aware of the fact that even out of an ignored 
        # folder a file could have been added using git add -f 
        find "$glob" -type f -exec \
            bash -c "FILE={};[ \$(git status -s \$FILE) == "" ] && echo \$FILE" \;
    else
        for file in "$glob" ; do
            # Again, be aware of files which add been added using -f
            bash -c "FILE={};[ \$(git status -s \$FILE) == "" ] && echo \$FILE" \;
        done
    fi
# Pipe stderr to /dev/null since .gitignore might contain entries for non 
# existing files which would trigger an error message when passing them to find
done < .gitignore 2>/dev/null | sort
Run Code Online (Sandbox Code Playgroud)

  • 我认为find是缺少指定的路径,应该是`find.-type f`不? (2认同)