List all files that are not ignored by .gitignore

后端 未结 2 1058
情歌与酒
情歌与酒 2021-02-13 18:37

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

相关标签:
2条回答
  • 2021-02-13 18:50
    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).

    0 讨论(0)
  • 2021-02-13 18:53

    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
    
    0 讨论(0)
提交回复
热议问题