如何更改git branch输出顺序

问题描述 投票:12回答:2

当我输入git branch时,我会收到一个分支列表,这些分支似乎按字母顺序排序,而不是按创建时间排序。

有没有办法使git branch的输出按日期排序?

git git-branch
2个回答
10
投票

编辑

唉,git-for-each-ref采取的排序选项存在明显问题。由于该命令(显然)明确地旨在显示refs并接受--sort选项,我认为这可能是一个错误[1]。

这是我可以进一步提出的最佳选择,但输出与原始格式相差甚远(因为它们依赖于事后的装饰修订来引用分支)。好吧,也许它对你有用:


[1]如果这是git-rev-listgit-log我会认为问题是我们实际上并没有走修正树;我们正在积极尝试只显示树木的提示,而不是走路。

Temporary alternative

git log --no-walk --date-order --oneline --decorate \
       $(git rev-list --branches --no-walk)

这会给你一个类似的列表

4934e92 (HEAD, origin/testing, origin/HEAD, testing) reviewed INSTALL file as per #1331
6215be7 (origin/maint, maint) reviewed INSTALL file as per #1331
1e5e121 (origin/emmanuel, emmanuel) buffers: adjust the size in zfsfuse_stat
e96783e (origin/compress, compress) buffers: adjust the size in zfsfuse_stat
f6e2c6c (origin/unstable, unstable) revert the fatal condition again
dd52720 (origin/master-lucid, master-lucid) lucid
3b32fa7 (tag: 0.7.0, origin/master, master) panic revocation of 0.7.0-0 package necessitates an update
6eaa64f (origin/maint-0.6.9, maint-0.6.9) Replace remount by drop_caches (on rollback)

_正如您所看到的,在存在许多远程(跟踪)分支的情况下,结果可能会有点压倒性,这些分支实际上是对相同的修订进行别名。但是,结果按(降序)日期正确排序。

The correct (unfortunately broken?) approach...

不,但你应该能做到

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

(使用--sort='-*authordate'作为作者日期订购)

在我的测试回购中,这会产生:

compress
emmanuel
maint
maint-0.6.9
master
master-lucid
testing
unstable

Alias

你可以创建一个git别名来执行此操作:将以下行附加到.git/config

[alias]
branch2 = git for-each-ref --sort='-*committerdate' --format="%(refname:short)" refs/heads/

从那以后,你可以说git branch2


2
投票

从git 2.7.0开始,这将有效:

git branch --sort=-committerdate

0
投票

Stujo's answer是我最喜欢的,但我想更进一步,按提交日期排序我的默认git branch行为。这是如何做:

git config --global branch.sort -committerdate

-之前删除committerdate以另一种方式排序。

现在git branch将始终按日期排序!

© www.soinside.com 2019 - 2024. All rights reserved.