How can I list the git subtrees on the root?

Following up on Sagi Illtus' answer, add the following alias to your ~/.gitconfig

[alias]
    ls-subtrees = !"git log | grep git-subtree-dir | awk '{ print $2 }'"

Then you can git ls-subtrees from the root of your repository to show all subtree paths:

$> cd /path/to/repository
$> git ls-subtrees
some/subtree/dir

There isn't any explicit way to do that (at least, for now), the only available commands are listed here (as you noted yourself, but here's a reference for future seekers): https://github.com/git/git/blob/master/contrib/subtree/git-subtree.txt

I went through the code (basically all this mechanism is a big shell script file), all of the tracking is done through commit messages, so all the functions use git log mechanism with lots of grep-ing to locate it's own data.

Since subtree must have a folder with the same name in the root folder of the repository, you can run this to get the info you want (in Bash shell):

git log | grep git-subtree-dir | tr -d ' ' | cut -d ":" -f2 | sort | uniq

Now, this doesn't check whether the folder exist or not (you may delete it and the subtree mechanism won't know), so here's how you can list only the existing subtrees, this will work in any folder in the repository:

 git log | grep git-subtree-dir | tr -d ' ' | cut -d ":" -f2 | sort | uniq | xargs -I {} bash -c 'if [ -d $(git rev-parse --show-toplevel)/{} ] ; then echo {}; fi'

If you're really up to it, propose it to Git guys to include in next versions:)


The problem with grepping the log is this tells you nothing about whether the subtree still exists or not. I've worked around this by simply testing the existence of the directory:

[alias]
        ls-subtrees = !"for i in $(git log | grep git-subtree-dir | sed -e 's/^.*: //g' | uniq); do test -d $i && echo $i; done"