如何获取按最近提交排序的Git分支列表

如何获取按最近提交排序的Git分支列表

技术背景

在使用Git进行项目开发时,经常需要查看各个分支的情况。有时候,我们希望能够快速了解哪些分支是最近有过提交的,以便优先处理这些活跃分支。这就需要对Git分支按最近提交进行排序。

实现步骤

基本用法

  • 使用 git for-each-ref 命令:
1
git for-each-ref --sort=-committerdate refs/heads/

此命令会按提交者日期降序排列本地分支。

  • 自Git 2.7.0版本起,也可以使用 git branch 命令:
1
2
git branch --sort=-committerdate  # 降序排列
git branch --sort=committerdate # 升序排列

高级用法

1
git for-each-ref --sort=committerdate refs/heads/ --format='%(HEAD) %(align:35)%(color:yellow)%(refname:short)%(color:reset)%(end) - %(color:red)%(objectname:short)%(color:reset) - %(align:40)%(contents:subject)%(end) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))'

该命令会以更详细和格式化的方式显示分支信息。

专业用法(Unix)

可以在 ~/.gitconfig 中添加以下别名:

1
2
3
[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 recentb 命令可以查看更详细的分支信息,包括与指定分支的提交差异等。

其他常用用法

  • 只显示分支名称:
1
git for-each-ref --count=30 --sort=-committerdate refs/heads/ --format='%(refname:short)'
  • 获取前五个按提交者日期排序的分支名称:
1
git branch --sort=-committerdate | head -5
  • git branch -vv 按提交日期排序并保留颜色等信息:
1
git branch -vv --color=always | while read; do echo -e $(git log -1 --format=%ct $(echo "_$REPLY" | awk '{print $2}' | perl -pe 's/\e\[?.*?[\@-~]//g') 2> /dev/null || git log -1 --format=%ct)"\t$REPLY"; done | sort -r | cut -f 2

核心代码

以下是一些核心的Git命令代码:

1
2
3
4
5
6
7
8
# 按提交者日期降序排列本地分支
git for-each-ref --sort=-committerdate refs/heads/

# 按提交者日期降序排列分支(Git 2.7.0及以上)
git branch --sort=-committerdate

# 配置全局默认按提交者日期降序排列分支
git config --global branch.sort -committerdate

最佳实践

  • 配置全局排序:如果经常需要按提交日期排序分支,可以设置全局配置:
1
git config --global branch.sort -committerdate

之后使用 git branch 命令时,默认就会按提交日期降序排列。

  • 使用别名:将常用的复杂命令设置为别名,方便使用。例如在 .gitconfig 中添加:
1
2
[alias]
br = !git branch --sort=committerdate --color=always | tail -n15

使用 git br 就可以查看最近的15个分支。

常见问题

版本兼容性问题

部分命令需要特定的Git版本支持,如 git branch --sort 自Git 2.7.0版本起可用。在使用前,请确保你的Git版本符合要求。

排序异常问题

如果排序结果不符合预期,可能是由于Git配置问题或数据异常。可以尝试更新Git版本、清理缓存或检查配置文件。

命令在不同系统中的差异

一些命令在不同的操作系统(如Windows和Unix)中可能需要进行适当调整,例如引号和转义字符的使用。在使用时要注意这些差异。


如何获取按最近提交排序的Git分支列表
https://119291.xyz/posts/how-to-get-git-branches-sorted-by-recent-commit/
作者
ww
发布于
2025年5月22日
许可协议