edi9999
edi9999

Reputation: 20554

List all files that are not ignored by .gitignore

I would like to list all files that are not ignored by .gitignore, ie all source files of my repository.

ag does it well by default, but I'm not aware of an approach that works without installing additional software.

git ls-files without options works almost well but doesn't take into account the files that have been modified/created, for example if I create a new file bar without commiting it, git ls-files doesn't show that file.

Upvotes: 18

Views: 6760

Answers (6)

turgenev
turgenev

Reputation: 13

-t option shows "tags" that indicate file status, and "R" indicates the file is already deleted. If we exclude files with both "R" and non-"R" line, we get what we want.

git ls-files -dcmot --exclude-standard | sed "s/^[HSMCK?U]/X/" | uniq | sed "s/^R/X/" | uniq -u | sed "s/^X //"

Upvotes: 0

danfuzz
danfuzz

Reputation: 4353

git ls-files --cached --others --exclude-standard will give you the info you want.

  • --cached -- Show files that git knows about. (Plain git ls-files with no options is equivalent to passing this option.)
  • --others -- Show files that git doesn't know about. This includes ignored files by default, but...
  • --exclude-standard -- Don't include ignored files in the output.

Upvotes: 7

Tomfox
Tomfox

Reputation: 145

Shorter variant of Uri's answer:

  • to find ignored files:
find . -not -path ./.git/\* -type f -exec git check-ignore -q {} \; -print
  • to find non-ignored files:
find . -not -path ./.git/\* -type f -not -exec git check-ignore -q {} \; -print

Upvotes: 0

Ahmad Ismail
Ahmad Ismail

Reputation: 13852

You can use fd.

The command to run is:

fd -H

Please note that in case there is no .git directory and only a .gitignore file (ex. bare repo), you have to specifically tell where the .gitignore file is.

fd -H --ignore-file .gitignore

For more details please check here.

Upvotes: 2

Uri London
Uri London

Reputation: 10797

Here is another way, using git check-ignore which you may find it cleaner:

find . -type f

will give you all the files in the current folder.

find . -type f -not -path './.git/*'

will give you all the files, with the exception of everything in .git folder, which you definitely don't want to include. then,

for f in $(find . -type f -not -path './.git/*'); do echo $f; done

is the same as above. Just a preparation to the next step, which introduce the condition. The idea is to use the git check-ignore -q which returns exit 0 when a path is ignored. Hence, here is the full command

for f in $(find . -type f -a -not -path './.git/*'); do
    if ! $(git check-ignore -q $f); then echo $f; fi
done

Upvotes: 4

Petr Skocik
Petr Skocik

Reputation: 60058

git status --short| grep  '^?' | cut -d\  -f2- 

will give you untracked files.

If you union it with git ls-files, you've got all unignored files:

( git status --short| grep '^?' | cut -d\  -f2- && git ls-files ) | sort -u

you can then filter by

( xargs -d '\n' -- stat -c%n 2>/dev/null  ||: )

to get only the files that are stat-able (== on disk).

Upvotes: 21

Related Questions