matt
matt

Reputation: 535202

single command to find all git ignored files and why they are being ignored

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.

Upvotes: 3

Views: 763

Answers (2)

jthill
jthill

Reputation: 60295

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.

Upvotes: 2

Arkadiusz Drabczyk
Arkadiusz Drabczyk

Reputation: 12393

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 **/*

Upvotes: 1

Related Questions