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

时间:2014-07-07 21:23:00

标签: git gitignore

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

我的项目有这个目录结构

.
├── aspnet
│   ├── .gitignore
│   ├── __init__.py
│   ├── lib
│   │   ├── <lots of big stuff>

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

3 个答案:

答案 0 :(得分:10)

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

find . -type f  | git check-ignore --stdin

find . -type f将以递归方式列出文件夹中的所有文件,而git check-ignore将列出列表中的这些文件,.gitignore实际上会忽略这些文件。


check-ignore命令相对较新。如果您的.git版本不支持它,则可以对POSIX兼容shell(如bash,sh,dash,zsh)使用以下解决方法。它基于.gitignore包含由shell解释的glob模式的事实。解决方法迭代来自.gitignore的glob模式,在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

答案 1 :(得分:2)

我的功夫用规则

递归列出所有被忽略的文件
find . -type d | grep -v .git | awk '{print $1"/"}' | git check-ignore -v --stdin

答案 2 :(得分:0)

在Windows PowerShell窗口中执行此操作很简单:

PS> Get-ChildItem -Recurse | Select-Object Fullname | git check-ignore --stdin -v

输出看起来确实很时髦,好像是为Cygwin或bash设计的(我不使用),因此这可能是我自己的错误配置,但对于找出为什么忽略该文件仍然很有帮助。 / p>

基于git 2.22.0.windows.1的评论