我想获得Git存储库中所有分支的列表,其中“最新”分支位于顶部,“最新的”分支是最近提交的分支(因此,更可能是我想关注的分支)。

有没有一种方法可以使用Git(a)按最新提交对分支列表进行排序,或者(b)以某种机器可读格式将分支列表与每个分支的最后提交日期一起获取?

最坏的情况是,我总是可以运行gitbranch获取所有分支的列表,解析其输出,然后运行git-log-n 1 branchname--format=format:%ci获取每个分支的提交日期。但这将在Windows环境下运行,在那里启动一个新进程相对昂贵,因此如果有很多分支,每个分支启动一次Git可执行文件可能会很慢。有没有一种方法可以用一个命令完成所有这些?


当前回答

这是我想要的变体:

git for-each-ref --sort=-committerdate --format='%(committerdate)%09%(refname:short)' refs/heads/ | tail -r

该tail-r反转列表,因此最近的commitedate是最后一个。

其他回答

仅供参考,如果您想获得最近签出的分支列表(而不是最近提交的分支),可以使用Git的reflog:

$ git reflog | egrep -io "moving from ([^[:space:]]+)" | awk '{ print $3 }' | head -n5
master
stable
master
some-cool-feature
feature/improve-everything

另请参阅:如何获取最近签出的Git分支列表?

对每个ref使用git的--sort=-committendate选项;

从Git 2.7.0开始,Git分支也可用:

基本用法:

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

结果:

Pro用法(Unix):

您可以在~/.gitconfig中放入以下代码段。recentb别名接受两个参数:

refbranch:前面和后面的列是根据哪个分支计算的。默认主机计数:显示最近的分支。默认值20

[alias]
    # ATTENTION: All aliases prefixed with ! run in /bin/sh make sure you use sh syntax, not bash/zsh or whatever
    recentb = "!r() { refbranch=$1 count=$2; git for-each-ref --sort=-committerdate refs/heads --format='%(refname:short)|%(HEAD)%(color:yellow)%(refname:short)|%(color:bold green)%(committerdate:relative)|%(color:blue)%(subject)|%(color:magenta)%(authorname)%(color:reset)' --color=always --count=${count:-20} | while read line; do branch=$(echo \"$line\" | awk 'BEGIN { FS = \"|\" }; { print $1 }' | tr -d '*'); ahead=$(git rev-list --count \"${refbranch:-origin/master}..${branch}\"); behind=$(git rev-list --count \"${branch}..${refbranch:-origin/master}\"); colorline=$(echo \"$line\" | sed 's/^[^|]*|//'); echo \"$ahead|$behind|$colorline\" | awk -F'|' -vOFS='|' '{$5=substr($5,1,70)}1' ; done | ( echo \"ahead|behind||branch|lastcommit|message|author\\n\" && cat) | column -ts'|';}; r"

结果:

Git分支名称列表,按最近提交排序…

根据Jakub的回答和Joe的提示,下面将去掉“refs/heads/”,因此输出仅显示分支名称:


命令:

git for-each-ref --count=30 --sort=-committerdate refs/heads/ --format='%(refname:short)'

结果:

我使用以下别名:

最近=“!r(){count=$1;每个ref的git--sort=-committedate-refs/heads--format='%(HEAD)%(颜色:黄色)%(refname:short)|%(颜色∶粗体绿色)%(committedate:relative)|%

其产生:

我们还可以给出自定义计数。,

git最近20(默认值为10)。

我能够参考前面的例子来创建最适合我的东西。

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)"'