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

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

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


当前回答

我发现以下命令对我的目的很有用。

git branch --sort=-committerdate | head -n 10

这将列出最新的10个分支。它很短,也可以在没有别名的情况下使用。

其他回答

我发现以下命令对我的目的很有用。

git branch --sort=-committerdate | head -n 10

这将列出最新的10个分支。它很短,也可以在没有别名的情况下使用。

下面是一个简单的命令,列出了具有最新提交的所有分支:

git branch -v

要按最近提交排序,请使用

git branch -v --sort=committerdate

资料来源:http://git-scm.com/book/en/Git-Branching-Branch-Management

被接受的命令行回答令人震惊,但如果你想要更漂亮的东西,比如GUI,以及你的origin==“github”。

您可以单击存储库中的“分支”。或者直接点击URL:https://github.com/ORGANIZATION_NAME/REPO_NAME/branches

我知道已经有很多答案了,但下面是我的两分钱,作为一个简单的别名(我喜欢在底部显示我最近的分支):

[alias]
        br = !git branch --sort=committerdate --color=always | tail -n15
[color "branch"]
        current = yellow
        local = cyan
        remote = red

这将为您提供最新15个分支的漂亮概览,颜色为,突出显示当前分支(并带有星号)。

对每个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"

结果: