# 告别复杂脚本，git 分支按日期排序

Original title: Sort branches by last commit date

> Hacker News story 49435285 · language: zh · submitted 2026-08-25
> Web version: https://zeli.app/zh/story/49435285
> About this feed: https://zeli.app/llms.txt · Agent guide: https://zeli.app/skill.md

▲ 137 · by [speckx](<https://news.ycombinator.com/user?id=speckx>) · 💬 50
[original](<https://ryangreenberg.com/til/git-branches-by-commit-date/>) · [HN discussion](<https://news.ycombinator.com/item?id=49435285>)

## Summary

我曾像许多人一样，因为 Git 分支太多而编写了复杂的脚本，每天运行多次来查看最新的分支。这个脚本通过循环和 sed 命令提取分支名，再结合 git log 输出日期进行排序，虽然有效但显得笨重。直到我发现现代 Git 版本早已内置了更优雅的解决方案。只需一条简单的配置命令 git config branch.sort committerdate，或者临时使用 git branch --sort committerdate，就能直接按最后提交日期排序分支。此外，Git 还支持多种排序字段，如 authordate、contents:size 等，功能远比我们想象的强大。

## Key quote

> 事实证明，在现代 Git 中，这完全是不必要的。

## HN discussion (50 comments)

### [rmunn](<https://news.ycombinator.com/item?id=49504860>)

> 我一直在使用 https://ses4j.github.io/2020/04/01/git-alias-recent-branches... 这篇帖子（虽然是 4 月 1 日写的，但并非愚人节玩笑）中建议的 `git lb` 别名，用起来非常顺手。它能按日期排序，展示我最近操作过的 10 个分支（修改别名定义以显示更多或更少的分支非常容易），最新的排在最前。这在各种场景下都很有用，尤其是我们内部的一个库大约每三个月就需要修复一个 bug 或添加一个新功能。所以每次我 cd 进那个仓库时，完全想不起上次做了什么，而 `git lb` 能立刻告诉我。

### [asb](<https://news.ycombinator.com/item?id=49502064>)

> 在我看来，更好的做法是 `git config branch.sort -committerdate`（注意那个 `-` 号）。这样会按最新的提交者日期优先排序。

### [ComputerGuru](<https://news.ycombinator.com/item?id=49502575>)

> 顺便提一下，Fish shell 在输入 git 命令时，会自动补全按最后提交日期排序的分支。它还提供了按类别分组的 commitish 对象补全，优先显示常用的目标，且每个组内都按各自的排序键进行排序。非常实用！

### [jbranchaud](<https://news.ycombinator.com/item?id=49504416>)

> 这类 git 分支脚本的最佳升级方案是将它们输出到 fzf。我一直使用的是 fzf 仓库里的这个 fbr 脚本：https://github.com/jbranchaud/dotfiles/blob/main/zshrc.local...

### [metmac](<https://news.ycombinator.com/item?id=49502769>)

> 为了实现这个目的，我是 `git recent` [0] 的超级粉丝。
>
> [0] - https://github.com/paulirish/git-recent

---

## Cite

- Page: https://zeli.app/zh/story/49435285
- Date: 2026-08-25
- Source: ryangreenberg.com (https://ryangreenberg.com/til/git-branches-by-commit-date/)
- Hacker News: https://news.ycombinator.com/item?id=49435285

## Other languages

- en: https://zeli.app/story/49435285 · markdown: https://zeli.app/story/49435285.md
- ja: https://zeli.app/ja/story/49435285 · markdown: https://zeli.app/ja/story/49435285.md
- ko: https://zeli.app/ko/story/49435285 · markdown: https://zeli.app/ko/story/49435285.md
- de: https://zeli.app/de/story/49435285 · markdown: https://zeli.app/de/story/49435285.md
- es: https://zeli.app/es/story/49435285 · markdown: https://zeli.app/es/story/49435285.md
- ru: https://zeli.app/ru/story/49435285 · markdown: https://zeli.app/ru/story/49435285.md
