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?
This command will list the files that are being tracked currently. If you want a list of files that ever existed use: git log --pretty=format: --name-only --diff-filter=A | sort - | sed '/^$/d'This command will list all the files including deleted files.
git ls-tree --full-tree --name-only -r HEAD | tree --fromfile .
This command will list the files that are being tracked currently. git log --pretty=format: --name-only --diff-filter=A | sort - | sed '/^$/d' This command will list all the files including deleted files.
In order to start tracking these files, we need to tell git which ones we want to track. We do this with the "git add " command. To track the "CHANGELOG.txt" file, I'll type "git add CHANGELOG.txt". Now, when I type "git status", we'll see the heading "Changes to be committed", and under that the message "new file: CHANGELOG.txt".
The files managed by git are shown by git ls-files. Check out its manual page. The accepted answer only shows files in the current directory's tree. To show all of the tracked files that have been committed (on the current branch), use --full-tree makes the command run as if you were in the repo's root directory. -r recurses into subdirectories.
Tracking Files in a Git Repository with "git add" | Modules Unraveled. 1 I'll type "vi Empty/test.txt" and press Enter. 2 I'll press the "i" key and type "This is a test document." 3 Then, quit the file by pressing the escape key and typing ":wq" and pressing Enter.
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:
.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?)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With