How to list all text (non-binary) files in a git repository?

前端 未结 3 1404
孤城傲影
孤城傲影 2020-12-09 04:14

I have a repository with a lot of autogenerated source files I\'ve marked as \"binary\" in .gitattributes (they are checked in because not everyone has access t

相关标签:
3条回答
  • 2020-12-09 04:27
    git grep --cached -Il ''
    

    lists all non-empty regular (no symlinks) text files:

    • -I: don't match the pattern in binary files
    • -l: only show the matching file names, not matching lines
    • '': the empty string makes git grep match any non-empty file
    • --cached: also find files added with git add but not yet committed (optional)

    Or you could use How to determine if Git handles a file as binary or as text? in a for loop with git ls-files.

    TODO empty files.

    Find all binary files instead: Find all binary files in git HEAD

    Tested on Git 2.16.1 with this test repo.

    0 讨论(0)
  • 2020-12-09 04:36

    The standard method for listing non-ignored files is:

    git ls-files --exclude-standard --cached
    

    But, as you seen, it lists all versioned files.

    One workaround could be to define in a separate file "exclude_binaries" an exclusion pattern in order to match all binaries that you know of.

    git ls-files --exclude-standard --cached \
    --exclude-from=/path/to/`exclude_binaries`
    

    That would be a less complex find, but it doesn't provide a fully automated way to list non-binary files: you still have to identify and list them in a separate pattern file.

    0 讨论(0)
  • 2020-12-09 04:39

    A clever hack to achieve this: listing all non-binary files that contains carriage returns

    $ git grep --cached -I -l -e $'\r'
    

    For my case, an empty string works better:

    $ git grep --cached -I -l -e $''
    

    Took it from git list binary and/or non-binary files?.

    0 讨论(0)
提交回复
热议问题