我想获得Git存储库中所有分支的列表,其中“最新”分支位于顶部,“最新的”分支是最近提交的分支(因此,更可能是我想关注的分支)。
有没有一种方法可以使用Git(a)按最新提交对分支列表进行排序,或者(b)以某种机器可读格式将分支列表与每个分支的最后提交日期一起获取?
最坏的情况是,我总是可以运行gitbranch获取所有分支的列表,解析其输出,然后运行git-log-n 1 branchname--format=format:%ci获取每个分支的提交日期。但这将在Windows环境下运行,在那里启动一个新进程相对昂贵,因此如果有很多分支,每个分支启动一次Git可执行文件可能会很慢。有没有一种方法可以用一个命令完成所有这些?
另一种变化:
git branch -r --sort=-committerdate --format='%(HEAD)%(color:yellow)%(refname:short)|%(color:bold green)%(committerdate:relative)|%(color:blue)%(subject)|%(color:magenta)%(authorname)%(color:reset)' --color=always | column -ts'|'
值得注意的是,尽管它正在查看远程分支中的更改,但在运行命令之前还是值得与origin同步(您可以使用Git fetch),因为我发现如果您的本地Git文件夹有一段时间没有更新,它可能会返回过期信息。
此外,这是一个在Windows cmd和PowerShell中工作的版本:
git branch -r --sort=-committerdate --format="%(HEAD)%(color:yellow)%(refname:short)|%(color:bold green)%(committerdate:relative)|%(color:blue)%(subject)|%(color:magenta)%(authorname)%(color:reset)" --color=always
我能够参考前面的例子来创建最适合我的东西。
git for each ref--sort=-提交日期refs/heads--format='%(authordate:短)%(颜色:红色)%(对象名称:短)%(%(颜色:绿色)%(提交日期:相对)%(颜色重置))'
正如下面评论中所建议的,您还可以包括远程分支和作者的姓名。
git for each ref--sort=-committedate refs/heads refs/remotes--format='%(authordate:短)%(颜色:红色)%(对象名称:短)%(%(颜色:绿色)%(提交日期:相对)%(颜色重置))%(作者姓名)'
下面是作为shell别名的两个命令,您可以轻松地将它们添加到shell概要文件中。
# show a list of local git branches sorted by the commit date
alias git.branches='git for-each-ref --sort=-committerdate refs/heads --format="%(authordate:short) %(color:red)%(objectname:short) %(color:yellow)%(refname:short)%(color:reset) (%(color:green)%(committerdate:relative)%(color:reset))"'
# show a list of local and remote git branches sorted by the commit date
alias git.branches.remote='git for-each-ref --sort=-committerdate refs/heads refs/remotes --format="%(authordate:short) %(color:red)%(objectname:short) %(color:yellow)%(refname:short)%(color:reset) (%(color:green)%(committerdate:relative)%(color:reset)) %(authorname)"'