How can I list the git subtrees on the root?

本小妞迷上赌 提交于 2019-11-28 16:59:44

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

miraculixx

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!