我想得到一个 Git 仓库中所有分支的列表,其中最新鲜的分支在顶部,其中最新鲜的分支是最近被提交的分支(因此更有可能是我想关注的分支)。
有什么办法可以让我用Git来(a)按照最新提交的时间对分支列表进行排序,或者(b)以某种机器可读的格式获得一个包含每个分支的最后提交日期的分支列表?
最坏的情况是,我可以运行 git branch
来获取所有分支的列表,解析其输出,然后对每个分支进行 git log -n 1 branchname --format=format:%ci
,以获取每个分支的提交日期。但这将在Windows盒子上运行,在那里启动一个新的进程是相对昂贵的,所以如果有很多分支的话,每个分支启动一次Git可执行文件会很慢。有什么办法可以用一条命令完成这些工作?
使用git for-each-ref
的-sort=-committerdate
选项。
也可自Git 2.7.0用于git branch
。
git for-each-ref --sort=-committerdate refs/heads/
# Or using git branch (since version 2.7.0)
git branch --sort=-committerdate # DESC
git branch --sort=committerdate # ASC
git for-each-ref --sort=committerdate refs/heads/ --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(color:red)%(objectname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))'