查找不在 .gitignore 中的文件

jcu*_*bic 17 grep find git wildcards

我有在我的项目中显示文件的 find 命令:

find . -type f -not -path './node_modules*' -a -not -path '*.git*' \
       -a -not -path './coverage*' -a -not -path './bower_components*' \
       -a -not -name '*~'
Run Code Online (Sandbox Code Playgroud)

如何过滤文件,使其不显示 .gitignore 中的文件?

我以为我使用:

while read file; do
    grep $file .gitignore > /dev/null && echo $file;
done
Run Code Online (Sandbox Code Playgroud)

但是 .gitignore 文件可以有 glob 模式(如果文件在 .gitignore 中,它也不适用于路径),如何根据可能有 glob 的模式过滤文件?

cuo*_*glm 12

git提供git-check-ignore检查文件是否被.gitignore.

所以你可以使用:

find . -type f -not -path './node_modules*' \
       -a -not -path '*.git*'               \
       -a -not -path './coverage*'          \
       -a -not -path './bower_components*'  \
       -a -not -name '*~'                   \
       -exec sh -c '
         for f do
           git check-ignore -q "$f" ||
           printf '%s\n' "$f"
         done
       ' find-sh {} +
Run Code Online (Sandbox Code Playgroud)

请注意,您将为此付出巨大代价,因为对每个文件都进行了检查。


Kus*_*nda 10

要显示结帐中的文件并由 Git 跟踪,请使用

$ git ls-files
Run Code Online (Sandbox Code Playgroud)

该命令有许多显示选项,例如缓存文件、未跟踪文件、修改文件、忽略文件等。请参见git ls-files --help

  • @cuonglm `-o`(其他)。例如,`git ls-files -o -X .gitignore` (2认同)

the*_*fog 9

有一个 git 命令可以做到这一点:例如

my_git_repo % git grep --line-number TODO                                                                                         
desktop/includes/controllers/user_applications.sh:126:  # TODO try running this without sudo
desktop/includes/controllers/web_tools.sh:52:   TODO: detail the actual steps here:
desktop/includes/controllers/web_tools.sh:57:   TODO: check if, at this point, the menurc file exists. i.e. it  was created
Run Code Online (Sandbox Code Playgroud)

正如您所说,它将执行基本的 grep 和大多数普通 grep 选项,但它不会搜索.git或您.gitignore文件中的任何文件或文件夹。
有关更多详细信息,请参阅man git-grep

子模块:

如果您在此 git repo 中还有其他 git repos(它们应该在子模块中),那么您也可以使用该标志--recurse-submodules在子模块中进行搜索


Mit*_*tar 6

我认为这很有效:

git ls-files --cached --modified --other --exclude-standard
Run Code Online (Sandbox Code Playgroud)

如果您还想递归到子模块,请添加--recurse-submodules.

  • 不,`--other`也会输出那些未被git跟踪的文件(然后`--exclude-standard`删除那些被git忽略的文件)。 (3认同)