问题
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?
回答1:
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?)
来源:https://stackoverflow.com/questions/34088711/how-to-list-all-distinct-extensions-of-tracked-files-in-a-git-repository