gitgit-subtree

How can I list the git subtrees on the root?


For example, you can do a git remote --verbose and git will show all the remotes you have on your project, git branch will show all the branches and signal the current branch, but how to list all subtrees, without any destructive command? git subtree will give the usage examples, but won't list anything. subtree only have add,pull,push,split,merge.


Solution

  • 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:)