You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Git清理分支靠时间区分活跃性,Mercurial如何按修改时间排序分支?

按修改时间排序Mercurial分支的实用方法

我之前也有过类似需求——需要快速区分Mercurial里活跃和老旧的分支,下面分享几个亲测好用的方法:

方法1:结合shell命令获取分支最后修改时间并排序

Mercurial的hg branches命令默认不显示分支的最后修改时间,我们可以通过遍历分支+查询分支最后提交的方式来实现排序:

hg branches | awk '{print $1}' | while read branch; do
  # 获取该分支最后一次提交的ISO时间和分支名
  echo "$(hg log -r "branch('$branch')" --template '{date|isodate} {branch}\n' | head -n1)"
done | sort -r

命令解释:

  • hg branches | awk '{print $1}':提取所有分支的名称
  • hg log -r "branch('$branch')" --template '{date|isodate} {branch}\n' | head -n1:获取指定分支的最后一次提交时间(ISO格式,方便排序)和分支名
  • sort -r:倒序排序,让最新修改的分支排在最顶部

方法2:直接查询分支头节点并排序

如果只需要关注未关闭分支的最新状态,可以用hg log直接筛选分支头节点并按时间排序:

hg log -r "head() and not closed()" --template '{branch}\t{date|isodate}\t{desc|firstline}\n' | sort -k2 -r

命令解释:

  • head() and not closed():筛选所有未关闭分支的头节点(每个分支的最新提交)
  • --template:自定义输出格式,包含分支名、修改时间和提交摘要
  • sort -k2 -r:按第二列(时间)倒序排序

如果需要包含已关闭的分支,去掉and not closed()即可。

进阶:设置命令别名

如果经常需要用这个功能,可以把命令加入Mercurial的别名,方便快速调用。编辑你的.hgrc文件,添加以下内容:

[alias]
# 未关闭分支按修改时间排序
branches-sorted = !hg log -r "head() and not closed()" --template '{branch}\t{date|isodate}\t{desc|firstline}\n' | sort -k2 -r
# 包含已关闭分支的版本
branches-sorted-all = !hg log -r "head()" --template '{branch}\t{date|isodate}\t{desc|firstline}\n' | sort -k2 -r

之后直接输入hg branches-sorted或hg branches-sorted-all就能快速查看排序后的分支列表了。

内容的提问来源于stack exchange,提问作者bluenote10

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:43:18