Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 402
jakub.g Avatar asked Dec 04 '15 12:12

jakub.g


People also ask

How do I see all tracked files in git?

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.

Which command provides the list of tracked files?

git ls-tree --full-tree --name-only -r HEAD | tree --fromfile .

How to list all files that are being tracked in Git?

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.

How do I track changes in a git repository?

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".

How to view all the files managed by a git repository?

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.

How to add a test file to a git repository?

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.


1 Answers

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?)

like image 104
jakub.g Avatar answered Oct 18 '22 10:10

jakub.g