git 本地分支列表如何根据最后一次提交(commit)时间进行排序(升序或降序)?
推荐方法
基本用法
git for-each-ref --sort=committerdate refs/heads/
其中,for-each-ref
表示打印每个引用的信息; --sort=committerdate
表示提交时间升序,即最久前最后提交的分支在前;若要提交时间降序,参数值加负号即可,--sort=-committerdate
。
如果 git 版本在 2.7.0 及以上,可以使用如下 branch 命令:
git branch --sort=committerdate
若按照最后一次 commit 时间降序,则 --sort=-committerdate
。
高级用法
如果觉得输出的信息不够详细或者不够直观,可以使用如下命令,增加可读性:
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))'