How can I make git list only the tracked directori

2020-08-17 18:11发布

问题:

Using git list-files gives me the directories and the files tracked within. Is there a command like:

git list-directories

or something similar that lists only the tracked non-empty non-recursive directory names?

回答1:

I think this will give you what you want:

git ls-files | xargs -n 1 dirname | uniq

So, take the output of git ls-files and pipe that into dirname, then uniq the results. This appears to work locally for me.



回答2:

git ls-tree -rt HEAD:./ | awk '{if ($2 == "tree") print $4;}'

If the files may contain spaces, we would have to play around with: Using awk to print all columns from the nth to the last which is not very fun.

-r makes it recurse, and -t makes it print trees when recursing, which is turned off by default.

I was unable to use ls-tree as mentioned at: https://stackoverflow.com/a/20247815/895245 because it is hard to deal with directories that have no files, just other directories.

This method also shows empty trees.

Tested on Git 2.19.0.



标签: git