3

I can find out what files are being ignored with

git status --ignored

I can find out why one file is being ignored with

git check-ignore -v somefile

How to combine the two? My feeble attempts don't work. Expressions like

git check-ignore -v .

or

get check-ignore -v **/*

come up way short; they list only a tiny fraction of the files listed in git status --ignored. (For example, .DS_Store files don't appear at all.)

The best I could come up with, with my feeble unix fu, is:

git status --ignored | tr -d '\t' | git check-ignore --verbose --stdin

But it errors out on the final empty line of the status output, and it seems nutty that I have to play these pipe games at all.

matt
  • 447,615
  • 74
  • 748
  • 977
  • There're many variants at https://stackoverflow.com/a/467053/7976758 (found in https://stackoverflow.com/search?q=%5Bgit%5D+list+all+ignored+files). For example `find . -type f -exec git check-ignore -v '{}' \+` or `git check-ignore -v $(find . -type f)` or `git check-ignore -v $(git ls-files --others)` – phd Apr 19 '20 at 14:53
  • You don't need the quotes, `find -type f -exec git check-ignore -v {} +`. – jthill Apr 19 '20 at 15:49
  • @phd I particularly like `git check-ignore -v $(find . -type f -print)` although I don't see how to exclude the `.git` folder as suggested by the comments. I still find it very bizarre there isn't just a direct git command. – matt Apr 19 '20 at 16:05
  • @jthill: some shells (csh/tcsh in particular) will eat the braces if they are not quoted. – torek Apr 19 '20 at 21:10
  • @matt: `find . -name .git -prune -o -type f -print` does the trick. – torek Apr 19 '20 at 21:11
  • @torek good and fair point, I've come to feel curmudgeonly about it tho, if people want to use oddbal shells I'm not gonna uglify my world to cater to them, they can keep their limitations and adapt the common syntax for themselves. – jthill Apr 19 '20 at 23:00

2 Answers2

1

In Bash 4+:

Enable globstar and dotglob:

shopt -s globstar
shopt -s dotglob

and:

git check-ignore -v **/*

In zsh globstar (known as recursive globbing) is on by default so just enable dotglob:

setopt dotglob

and:

git check-ignore -v **/*
Arkadiusz Drabczyk
  • 8,447
  • 2
  • 14
  • 29
  • The problem with `**/*` (as it's demonstrated in the question) is it skips dot-files (`.*`). – phd Apr 19 '20 at 16:25
1

The swiss-army-knife of git aware file listing is git ls-files.

git ls-files --exclude-standard -oi

will list all unindexed ignored files. And git check-ignore has a --stdin option, so...

git ls-files --exclude-standard -oi | git check-ignore -vn --stdin

does what you want, about as fast as possible. I want that --exclude-standard option often enough that I have a global alias for it, git config --global alias.ls "ls-files --exclude-standard", so for me it'd be git ls -oi | git check-ignore -vn --stdin.

jthill
  • 42,819
  • 4
  • 65
  • 113
  • Yup, I like that best, thanks. Still don't quite see why no single git command just _does_ this. – matt Apr 19 '20 at 22:09
  • I'd say because it's easily constructible with two existing git commands, each of them useful without the other. – jthill Apr 19 '20 at 22:55