Search code examples
gitbashgrepgitattributes

How to list all distinct extensions of tracked files in a git repository?


I'd like to know all distinct extensions of files tracked by git in a given repo, in order to create appropriate .gitattributes file.

Example output expected:

bat
gitignore
gradle
html
jar
java
js
json
md
png
properties
py
svg
webp
xml
yml

What command can I use for that?


Solution

  • git ls-tree -r HEAD --name-only | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort -u 
    

    When you declare it as an alias, you have to escape $1:

    alias gitFileExtensions="git ls-tree -r HEAD --name-only | perl -ne 'print \$1 if m/\.([^.\/]+)$/' | sort -u"
    

    This is better than naive find, because:

    • it excludes untracked (gitignored) files
    • it excludes .git directory which contains usually hundreds/thousands of files and hence slows down the search

    (inspired by How can I find all of the distinct file extensions in a folder hierarchy?)