ChatGPT解决这个技术问题 Extra ChatGPT

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

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

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

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

stackoverflow.com/a/2514279/1804124 有更好的答案。
@Spundun,你把我弄丢了。多个命令的组合,包括通过 perl 和 sed 管道传输的内容,如何比使用 Git 已有的命令“更好”?
因为有了这里的答案,我没有得到 repo 中的所有分支。在我的特殊情况下,答案会给我一个分支,而那里的答案给了我 20 个左右的分支(使用 -r 选项)。
@Spundun 关于来自 Jakub Narębski 的 git for-each-ref 的答案:您可以让远程分支通过 refs/remotes/ 而不是 refs/heads/ (或者您可以同时通过两者,空格分隔); refs/tags/ 用于标记,或者仅 refs/ 用于所有三种。
从 git 2.7(2015 年第四季度)开始,不再有 for-each-ref!您将直接使用 git branch --sort=-committerdate:参见 my answer below

n
nocnokneo

使用 git for-each-ref--sort=-committerdate 选项;

也可用于 git branchsince Git 2.7.0

基本用法:

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

结果:

https://i.imgur.com/AlaP8dD.png

高级用法:

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))'

结果:

https://i.imgur.com/tDCTiZx.png

专业用途(Unix):

您可以将以下代码段放入您的 ~/.gitconfig。最近的别名接受两个参数:

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"

结果:

https://i.stack.imgur.com/Q8Buq.png


完美的!我什至可以通过附加 --format=%(refname) 将输出限制为仅引用名称。
这对我来说更好:git for-each-ref --sort=-committerdate refs/heads/ --format='%(refname) %(committerdate) %(authorname)' | sed 's/refs\/heads\///g'
@ilius:为什么不使用 :shortname
@ilius:正如@BeauSmith 所写:git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads/。 git-for-each-ref(1) 手册页说:对于 ref 的明确短名称附加 :short
这是一个彩色版本,包括散列、消息、基于提交日期的升序排序,以及每个分支上最后一次提交的相对年龄。我从上面的你们那里偷走了所有的想法。它在我的 [alias] 部分的 .gitconfig 中,我喜欢它。 br = 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))'
P
Peter Mortensen

Git 分支名称列表,按最近提交排序...

Jakub’s answerJoe’s tip 上展开,以下内容将去除“refs/heads/”,因此输出仅显示分支名称:

命令:

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

结果:

https://i.stack.imgur.com/MYOCB.png


您也可以使用 --format=%(refname:short) 而不是依赖 cut
有没有办法为远程存储库做到这一点?
aah - @jakub.g 已经解释过:你可以让远程分支通过 refs/remotes/ 而不是 refs/heads/。完美的!!
现在您可以使用 git branch 执行此操作,因此获取本地、远程或所有分支的工作方式类似于 git-branch(即 -r、-a)。 git branch -r --sort=committerdate --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(color:red)%(objectname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))'
@AllanBowe 以下将输出 repo 中的前 5 个活动分支:git branch -va --sort=committerdate | tail -5。也许这是您所询问和发现的替代方案。
B
Ben Claar

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

git branch -v

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

git branch -v --sort=committerdate

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


git branch -av 如果您也想查看非本地分支机构。
git branch -v 包含列出的每个提交的日期是否容易?
这太棒了。我喜欢 git branch -va --sort=-committerdate 显示非本地分支,最近更改的分支位于顶部。
git branch -v --format='%(committerdate:short) %(refname:short)' 将包括日期。
n
nikolay

这是最佳代码,它结合了其他两个答案:

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

甚至稍微优化以获得表格输出:git for-each-ref --sort=-committerdate refs/heads/ --format='%(committerdate:short) %(authorname) %(refname:short)'
出于某种原因,我不得不在 Windows 上使用双引号,但除此之外这些都可以正常工作:)
@schoetbi 该代码看起来与 nikolay 的代码一模一样,您进行了哪些更改以使其成为表格?
@Enrico 和其他可能想知道同一件事的人。尼古拉用 schoetbis 的建议改变了他的答案。通过首先移动总是相同长度的日期,结果看起来更像表格。
d
dimid

我使用以下别名:

recent = "!r() { count=$1; git for-each-ref --sort=-committerdate refs/heads --format='%(HEAD)%(color:yellow)%(refname:short)|%(color:bold green)%(committerdate:relative)|%(color:blue)%(subject)|%(color:magenta)%(authorname)%(color:reset)' --color=always --count=${count:=10} | column -ts'|'}; r"

产生:

https://i.stack.imgur.com/iGn5R.png

我们还可以给出自定义计数,例如,

git recent 20(默认值为 10)。


至少在最新版本的 git 中,您只需在格式字符串的开头添加 '%(HEAD) ...' 即可获得相同的效果,而无需通过 sed 命令进行管道传输
我无法将其用作 git 别名。我不得不使用 [alias] recent = !git for-each-ref --sort=-committerdate refs/heads --format='%(HEAD)%(color:yellow)%(refname:short)|%(color:bold green)%(committerdate:relative)|%(color:blue)%(subject)|%(color:magenta)%(authorname)%(color:reset)'|column -ts'|'
我必须添加 --color=always 才能获得颜色。 git for-each-ref --sort=-committerdate refs/heads --format='%(HEAD)%(color:yellow)%(refname:short)|%(color:bold green)%(committerdate:relative)|%(color:blue)%(subject)|%(color:magenta)%(authorname)%(color:reset)' --color=always|column -ts'|'}
@mwfearnley:对我来说,在大括号内放一个分号有助于!r(){git for-each-ref ... ;}; r
我还需要在 -ts'|' 之后添加一个分号,即 "!r(){ git ... -ts'|'; }; r"
A
Andrew

我能够参考前面的示例来创建最适合我的东西。

git for-each-ref --sort=-committerdate refs/heads --format='%(authordate:short) %(color:red)%(objectname:short) %(color:yellow)%(refname:short) %(color:reset) (%(color:green)%(committerdate:relative)%(color:reset))'

https://i.stack.imgur.com/XrfBr.png

正如下面评论中所建议的,您还可以包括远程分支和作者姓名。

git for-each-ref --sort=-committerdate refs/heads refs/remotes --format='%(authordate:short) %(color:red)%(objectname:short) %(color:yellow)%(refname :short)%(color:reset) (%(color:green)%(committerdate:relative)%(color:reset)) %(authorname)'

https://i.stack.imgur.com/mKIDj.png

这两个命令都作为 shell 别名,您可以轻松地将它们添加到 shell 配置文件中。

# show a list of local git branches sorted by the commit date
alias git.branches='git for-each-ref --sort=-committerdate refs/heads --format="%(authordate:short) %(color:red)%(objectname:short) %(color:yellow)%(refname:short)%(color:reset) (%(color:green)%(committerdate:relative)%(color:reset))"'

# show a list of local and remote git branches sorted by the commit date
alias git.branches.remote='git for-each-ref --sort=-committerdate refs/heads refs/remotes --format="%(authordate:short) %(color:red)%(objectname:short) %(color:yellow)%(refname:short)%(color:reset) (%(color:green)%(committerdate:relative)%(color:reset)) %(authorname)"'

这看起来不错,比我在 stackoverflow.com/a/33163401/6309 中建议的直接使用 git branch 更丰富多彩。 +1
谢谢,@VonC 很高兴你喜欢它!
同上 - 这个开箱即用,不像我刚刚尝试过的其他一些。谢谢。
这个干净整洁。有时我会这样添加远程和作者名:git for-each-ref --sort=-committerdate refs/heads/ refs/remotes --format='%(authordate:short) %(authorname) %(color:red)%(objectname:short) %(color:yellow)%(refname:short)%(color:reset) (%(color:green)%(committerdate:relative)%(color:reset))'
像这个!简洁明了,谢谢。 :)
V
VonC

git 2.7(2015 年第四季度)将直接使用 git branch 引入分支排序:
请参阅 commit aa3bc55commit aedcb7dcommit 1511b22commit f65f139、...(2015 年 9 月 23 日)、commit aedcb7d、{ 3}、commit ca41799(2015 年 9 月 24 日)和 commit f65f139、...(2015 年 9 月 23 日),作者为 Karthik Nayak (KarthikNayak)
(由 commit 7f11b48 中的 Junio C Hamano -- gitster -- 合并,2015 年 10 月 15 日)

特别是,commit aedcb7d

branch.c:使用 'ref-filter' API

使“branch.c”使用“ref-filter”API 来遍历引用排序。这将删除“branch.c”中使用的大部分代码,将其替换为对“ref-filter”库的调用。

adds the option --sort=<key>

根据给定的键排序。前缀 - 按值的降序排序。您可以多次使用 --sort= 选项,在这种情况下,最后一个键成为主键。支持的键与 git for-each-ref 中的键相同。排序顺序默认为基于完整的 refname(包括 refs/... 前缀)进行排序。这首先列出了分离的 HEAD(如果存在),然后是本地分支,最后是远程跟踪分支。

这里:

git branch --sort=-committerdate 

或者(参见下面的 Git 2.19)

# if you are sure to /always/ want to see branches ordered by commits:
git config --global branch.sort -committerdate
git branch

另请参阅 Karthik Nayak (KarthikNayak)commit 9e46833(2015 年 10 月 30 日)。
帮助:Junio C Hamano (gitster)
(由 Junio C Hamano -- gitster --commit 415095f 中合并,2015 年 11 月 3 日)

当按数值排序时(例如 --sort=objectsize),当两个 refs 保持相同的值时,没有后备比较。正如 Johannes Sixt ($gmane/280117) 指出的那样,这可能会导致意外结果(即无法预先确定具有相同值的 ref 的列出顺序)。因此,只要其他标准相等,就回退到基于 refname 的字母比较。 $ git branch --sort=objectsize * (HEAD 与 fromtag 分离) 分支-二分支-一主

在 Git 2.19 中,可以默认设置排序顺序。
git branch 支持配置 branch.sort,如 git tag,它已经有配置 tag.sort
请参阅 commit 560ae1c(8 月 16 日2018 年),作者为 Samuel Maftoul (``)
(由 Junio C Hamano -- gitster --commit d89db6f 合并,2018 年 8 月 27 日)

分支排序:

当由 git-branch 显示时,此变量控制分支的排序顺序。如果没有提供“--sort=”选项,此变量的值将用作默认值。

要列出远程分支,请使用 git branch -r --sort=objectsize-r 标志使它列出远程分支而不是本地分支。

在 Git 2.27(2020 年第二季度)中,“git branch”和其他“for-each-ref”变体接受了多个 --sort=<key> 选项(按优先级递增的顺序),但它在“--ignore-case”处理方面存在一些缺陷,并且并列- 使用已修复的 refname 中断。

请参阅 Jeff King (peff)commit 7c5045fcommit 76f9e56(2020 年 5 月 3 日)。
(由 Junio C Hamano -- gitster --commit 6de1630 中合并,2020 年 5 月 8 日)

ref-filter:仅在所有用户排序后应用后备 refname 排序签名者:Jeff King

提交 9e468334b4(“ref-filter:按字母顺序比较的回退”,2015-10-30,Git v2.7.0-rc0 - 第 10 批中列出的合并)教导 ref-filter 的排序回退到比较 refnames。但它是在错误的级别上做的,覆盖了来自用户的单个“--sort”键的比较结果,而不是在所有排序键都用完之后。这适用于单个“--sort”选项,但不适用于多个选项。我们将打破第一个键与 refname 的任何联系,并且根本不评估第二个键。为了让事情变得更有趣,我们有时只应用了这个后备!对于像“taggeremail”这样需要字符串比较的字段,我们会真正返回 strcmp() 的结果,即使它是 0。但是对于像“taggerdate”这样的数字“值”字段,我们确实应用了回退。这就是为什么我们的多重排序测试错过了这一点:它使用 taggeremail 作为主要比较。因此,让我们从添加一个更严格的测试开始。我们将有一组提交,表达两个标记电子邮件、日期和参考名称的每个组合。然后我们可以确认我们的排序以正确的优先级应用,我们将同时命中字符串和值比较器。这确实显示了错误,并且修复很简单:在所有 ref_sorting 键都已用尽之后,将后备移动到外部 compare_refs() 函数。请注意,在外部函数中,我们没有“ignore_case”标志,因为它是每个单独的 ref_sorting 元素的一部分。这种后备应该做什么是有争议的,因为我们没有使用用户的键来匹配。但直到现在,我们一直在努力尊重这面旗帜,所以最小侵入性的事情就是尝试继续这样做。由于当前代码中的所有调用者要么为所有键设置标志,要么不为任何键设置标志,我们可以从第一个键中提取标志。在一个假设的世界中,用户确实可以单独翻转不区分大小写的键,我们可能希望扩展代码以将该案例与笼统的“--ignore-case”区分开来。

git branch --sort(man) wrt 分离的 HEAD 显示器的实现一直很麻烦,已使用 Git 2.31(2021 年第一季度)进行了清理。

请参阅 Ævar Arnfjörð Bjarmason (avar)commit 4045f65commit 2708ce6commit 7c269a7commit d094748commit 75c50e5(2021 年 1 月 7 日)和 commit 08bf6a8commit ffdd02a(2021 年 1 月 6 日)。
(由 Junio C Hamano -- gitster -- 于 2021 年 1 月 25 日在 commit 9e409d7 中合并)

分支:首先在反向排序下显示“HEAD detached”签名者:Ævar Arnfjörð Bjarmason

更改“git branch -l --sort=-objectsize”(man) 之类的输出,以在输出开头显示“(HEAD detached at )”消息。在之前的提交中添加 compare_detached_head() 函数之前,我们会将此输出作为紧急效果发出。出于排序目的考虑“(HEAD detached at )”消息的对象大小、类型或其他非属性没有任何意义。让我们总是在顶部发出它。它首先被排序的唯一原因是因为我们将它注入到 ref-filter 机器中,所以 builtin/branch.c 不需要自己做“我分离了吗?”检测。

使用 Git 2.35(2022 年第一季度),类似“git -c branch.sort=bogus branch new HEAD(man),即
git branch(man) 的操作模式 命令不需要排序键信息,不再因看到 伪造的排序键而出错。

请参阅 Junio C Hamano (gitster)commit 98e7ab6commit 1a89796(2021 年 10 月 20 日)。
(由 Junio C Hamano -- gitster --commit 5126145 中合并,2021 年 11 月 29 日)

for-each-ref: 延迟解析 --sort= 选项

for-each-ref 系列命令在看到每个 --sort= 选项时立即调用解析器,当 无法识别时,甚至在命令行上看到其他选项之前就死掉了。相反,将它们累积在一个字符串列表中,并在命令行解析完成后将它们解析为 ref_sorting 结构。因此,“git branch --sort=bogus -h”(man) 过去无法提供简短的帮助,这可以说是一个功能,现在这样做了,这与其他选项的工作方式更加一致。


要使用此选项列出遥控器,请添加 -r
P
Peter Mortensen

我还需要没有任何重复的颜色、标签和远程引用:

for ref in $(git for-each-ref --sort=-committerdate --format="%(refname)" refs/heads/ refs/remotes ); do git log -n1 $ref --pretty=format:"%Cgreen%cr%Creset %C(yellow)%d%Creset %C(bold blue)<%an>%Creset%n" | cat ; done | awk '! a[$0]++'

因为引用可能很难,这里是 Bash 的别名:

alias glist='for ref in $(git for-each-ref --sort=-committerdate --format="%(refname)" refs/heads/ refs/remotes ); do git log -n1 $ref --pretty=format:"%Cgreen%cr%Creset %C(yellow)%d%Creset %C(bold blue)<%an>%Creset%n" | cat ; done | awk '"'! a["'$0'"]++'"

$ awk: 第 1 行附近的语法错误 awk: 在第 1 行附近退出
@GotNoSugarBaby您正在使用示例中的单引号对吗?你用的是哪个外壳?否则,Bash 会赋予该字符特殊的含义。
嘿,我在 /bin/bash (GNU bash,版本 4.0.28(1)-release (i386-pc-solaris2.11))上运行了这个,并直接复制并粘贴了你的示例 - 但从那时起我就运行了它在 /bin/bash (GNU bash,版本 3.2.48(1)-release (x86_64-apple-darwin12))上并且有效,所以我将删除反对票。非常感谢埃斯塔尼。
@GotNoSugarBaby 我使用 4.2.25(1)-release (x86_64-pc-linux-gnu) 并在 3.x 上尝试过并且工作正常。我不确定它是什么问题......但它可能是一个与 git 版本相关的问题,而不是一个 bash 问题。无论如何,我很高兴它对你有用!
@MichaelDiscenza 只是将所有内容都传递给头部。那将是在末尾添加 | head -n20 。如果您使用的是别名,请确保它在 in 引号内。
P
Peter Mortensen

从 Git 2.19 开始,您可以简单地:

git branch --sort=-committerdate

你也可以:

git config branch.sort -committerdate

因此,每当您列出当前存储库中的分支时,它将按提交日期排序。

如果每当您列出分支时,您希望它们按提交日期排序:

git config --global branch.sort -committerdate

免责声明:我是 Git 中这个功能的作者,我在看到这个问题时实现了它。


最新的答案,比使用复杂的脚本或别名要容易得多👌
谨慎使用!大家注意,这不是列出分支的命令。这是一个更改 Git 配置的命令,将产生永久性的全球影响。
@Jazimov你是对的,我编辑了答案,所以它更清楚
感谢您添加这个简洁的功能,@smaftoul,我经常使用它。 ; )
@theartofrain 不客气,我也经常使用它;)
J
John Mellor

其他答案似乎不允许传递 -vv 以获得详细输出。

因此,这是一个按提交日期、保留颜色等对 git branch -vv 进行排序的单行代码:

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 branch -vv --color=always | while read; do echo -e $(git log -1 --format=%ci $(echo "_$REPLY" | awk '{print $2}' | perl -pe 's/\e\[?.*?[\@-~]//g') 2> /dev/null || git log -1 --format=%ci)" $REPLY"; done | sort -r | cut -d ' ' -f -1,4-

样本输出:

2013-09-15   master                  da39a3e [origin/master: behind 7] Some patch
2013-09-11 * (detached from 3eba4b8) 3eba4b8 Some other patch
2013-09-09   my-feature              e5e6b4b [master: ahead 2, behind 25] WIP

分成多行可能更具可读性:

git branch -vv --color=always | while read; do
    # The underscore is because the active branch is preceded by a '*', and
    # for awk I need the columns to line up. The perl call is to strip out
    # ansi colors; if you don't pass --color=always above you can skip this
    local branch=$(echo "_$REPLY" | awk '{print $2}' | perl -pe 's/\e\[?.*?[\@-~]//g')
    # git log fails when you pass a detached head as a branch name.
    # Hide the error and get the date of the current head.
    local branch_modified=$(git log -1 --format=%ci "$branch" 2> /dev/null || git log -1 --format=%ci)
    echo -e "$branch_modified $REPLY"
# cut strips the time and timezone columns, leaving only the date
done | sort -r | cut -d ' ' -f -1,4-

这也应该与 git branch 的其他参数一起使用,例如 -vvr 列出远程跟踪分支,或 -vva 列出远程跟踪和本地分支。


-vv 确实很有用,谢谢。但是,此解决方案仍会为每个分支生成新进程,这是 OP 想要避免的。
实际上git branch并没有具体定义-vv的含义,而只是定义了-v的含义,所以-vv应该与-v相同。
这是最好的。添加 -avv 使其也考虑到远程分支。谢谢你!
@musiphil 我的 git 分支 手册页,第 -v, -vv, --verbose 部分包含以下内容:If given twice, print the name of the upstream branch, as well
@Perleone:我不知道我是如何得到这些信息的,但你是对的,我是正确的。谢谢!
P
Peter Mortensen

我喜欢使用相对日期并像这样缩短分支名称:

git for-each-ref --sort='-authordate:iso8601' --format=' %(authordate:relative)%09%(refname:short)' refs/heads

这给了你输出:

21 minutes ago  nathan/a_recent_branch
6 hours ago     master
27 hours ago    nathan/some_other_branch
29 hours ago    branch_c
6 days ago      branch_d

我建议制作一个 Bash 文件来添加所有您喜欢的别名,然后将脚本分享给您的团队。这是一个仅添加此示例的示例:

#!/bin/sh

git config --global alias.branches "!echo ' ------------------------------------------------------------' && git for-each-ref --sort='-authordate:iso8601' --format=' %(authordate:relative)%09%(refname:short)' refs/heads && echo ' ------------------------------------------------------------'"

然后,您可以这样做以获得格式良好且排序良好的本地分支列表:

git branches

更新:如果您想着色,请执行此操作:

#!/bin/sh
#
(echo ' ------------------------------------------------------------‌​' && git for-each-ref --sort='-authordate:iso8601' --format=' %(authordate:relative)%09%(refname:short)' refs/heads && echo ' ------------------------------------------------------------‌​') | grep --color -E "$(git rev-parse --abbrev-ref HEAD)$|$"

这给了我fatal: unknown field name: '-authordate:iso8601'
花哨的彩色输出很漂亮,但这很简单,正是我想要的。将 refs/heads 替换为 refs/remotes 以查看远程分支。
命令本身很可爱,但别名抛出错误:expansion of alias 'branches' failed; 'echo' is not a git command
为我工作。如果您只是将其复制粘贴到终端会发生什么? (echo ' ------------------------------------------------------------‌​' && git for-each-ref --sort='-authordate:iso8601' --format=' %(authordate:relative)%09%(refname:short)' refs/heads && echo ' ------------------------------------------------------------‌​') | grep --color -E "$(git rev-parse --abbrev-ref HEAD)$|$"
P
Peter Mortensen

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

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

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


git config --global alias.br "! git branch --sort=-committerdate | head -n 10"
e
epylinkn

添加一些颜色(因为 pretty-format 不可用)

[alias]
    branchdate = for-each-ref --sort=-committerdate refs/heads/ --format="%(authordate:short)%09%(objectname:short)%09%1B[0;33m%(refname:short)%1B[m%09"

b
bdesham

我想出了以下命令(适用于 Git 2.13 及更高版本):

git branch -r --sort=creatordate \
    --format "%(creatordate:relative);%(committername);%(refname:lstrip=-1)" \
    | grep -v ";HEAD$" \
    | column -s ";" -t

如果您没有 column,您可以将最后一行替换为

    | sed -e "s/;/\t/g"

输出看起来像

6 years ago             Tom Preston-Werner  book
4 years, 4 months ago   Parker Moore        0.12.1-release
4 years ago             Matt Rogers         1.0-branch
3 years, 11 months ago  Matt Rogers         1.2_branch
3 years, 1 month ago    Parker Moore        v1-stable
12 months ago           Ben Balter          pages-as-documents
10 months ago           Jordon Bedwell      make-jekyll-parallel
6 months ago            Pat Hawks           to_integer
5 months ago            Parker Moore        3.4-stable-backport-5920
4 months ago            Parker Moore        yajl-ruby-2-4-patch
4 weeks ago             Parker Moore        3.4-stable
3 weeks ago             Parker Moore        rouge-1-and-2
19 hours ago            jekyllbot           master

我写了 a blog post 关于各个部分的工作原理。


好的。 +1。它确实使用了我在 stackoverflow.com/a/33163401/6309 中提到的 git branch --sort
@DanNissenbaum 确保您使用的是 Git 2.13(2017 年 5 月发布)或更高版本。
Z
ZenoArrow

另一种变化:

git branch -r --sort=-committerdate --format='%(HEAD)%(color:yellow)%(refname:short)|%(color:bold green)%(committerdate:relative)|%(color:blue)%(subject)|%(color:magenta)%(authorname)%(color:reset)' --color=always | column -ts'|'

值得注意的是,即使它正在查看远程分支中的更改,也值得在运行命令之前与 origin 同步(您可以使用 Git fetch),因为我发现如果您的本地 Git 文件夹没有,它可以返回过期信息过段时间更新了。

此外,这是一个适用于 Windows cmdPowerShell 的版本:

git branch -r --sort=-committerdate --format="%(HEAD)%(color:yellow)%(refname:short)|%(color:bold green)%(committerdate:relative)|%(color:blue)%(subject)|%(color:magenta)%(authorname)%(color:reset)" --color=always

感谢您对 Windows Cmd/Powershell 的回答。我可以确认它也适用于 Cmder。
R
Ron DeVera

我遇到了同样的问题,所以我编写了一个名为 Twig 的 Ruby gem。它按时间顺序列出分支(最新的在前),还可以让您设置最大年龄,这样您就不会列出所有分支(如果您有很多分支)。例如:

$ twig

                              issue  status       todo            branch
                              -----  ------       ----            ------
2013-01-26 18:00:21 (7m ago)  486    In progress  Rebase          optimize-all-the-things
2013-01-26 16:49:21 (2h ago)  268    In progress  -               whitespace-all-the-things
2013-01-23 18:35:21 (3d ago)  159    Shipped      Test in prod  * refactor-all-the-things
2013-01-22 17:12:09 (4d ago)  -      -            -               development
2013-01-20 19:45:42 (6d ago)  -      -            -               master

它还允许您存储每个分支的自定义属性,例如工单 ID、状态、待办事项,并根据这些属性过滤分支列表。更多信息:http://rondevera.github.io/twig/


该名称可能无济于事,因为我很确定那里有一些具有相同名称的软件。
P
Peter Mortensen

仅供参考,如果您想获得最近签出的分支列表(而不是最近提交的),您可以使用 Git 的 reflog:

$ git reflog | egrep -io "moving from ([^[:space:]]+)" | awk '{ print $3 }' | head -n5
master
stable
master
some-cool-feature
feature/improve-everything

另请参阅:How can I get a list of Git branches that I've recently checked out?


P
Peter Mortensen

这是我用来在最近的分支之间切换的小脚本:

#!/bin/bash
# sudo bash

re='^[0-9]+$'

if [[ "$1" =~ $re ]]; then
    lines="$1"
else
    lines=10
fi
branches="$(git recent | tail -n $lines | nl)"
branches_nf="$(git recent-nf | tail -n $lines | nl)"
echo "$branches"

# Prompt which server to connect to
max="$(echo "$branches" | wc -l)"
index=
while [[ ! ( "$index" =~ ^[0-9]+$ && "$index" -gt 0 && "$index" -le "$max" ) ]]; do
    echo -n "Checkout to: "
    read index
done

branch="$( echo "$branches_nf" | sed -n "${index}p" | awk '{ print $NF }' )"
git co $branch
clear

使用这两个别名:

recent = for-each-ref --sort=committerdate refs/heads/ --format=' %(color:blue) %(authorname) %(color:yellow)%(refname:short)%(color:reset)'
recent-nf = for-each-ref --sort=committerdate refs/heads/ --format=' %(authorname) %(refname:short)'

只需在 Git 存储库中调用它,它就会显示最后 N 个分支(默认为 10 个)以及每个分支旁边的一个数字。输入分支的编号,它会检查:

https://i.stack.imgur.com/pvuZq.png


请查看Why not upload images of code/errors when asking a question?(例如,“图片应仅用于说明问题 无法以任何其他方式明确 i>例如提供用户界面的屏幕截图。”)并采用适当的 action(它也包括答案)。
V
Victor Choy

通常我们最近会考虑远程分支。所以试试这个

git fetch
git for-each-ref --sort=-committerdate refs/remotes/origin

P
Peter Mortensen

这是另一个脚本,它执行所有其他脚本所做的事情。实际上,它为您的 shell 提供了一个功能。

它的贡献在于它从你的 Git 配置中提取了一些颜色(或使用默认值)。

# Git Branch by Date
# Usage: gbd [ -r ]
gbd() {
    local reset_color=`tput sgr0`
    local subject_color=`tput setaf 4 ; tput bold`
    local author_color=`tput setaf 6`

    local target=refs/heads
    local branch_color=`git config --get-color color.branch.local white`

    if [ "$1" = -r ]
    then
        target=refs/remotes/origin
        branch_color=`git config --get-color color.branch.remote red`
    fi

    git for-each-ref --sort=committerdate $target --format="${branch_color}%(refname:short)${reset_color} ${subject_color}%(subject)${reset_color} ${author_color}- %(authorname) (%(committerdate:relative))${reset_color}"
}

P
Peter Mortensen

仅获取根据提交者日期排序的前五个分支名称:

git branch --sort=-committerdate | head -5


P
Peter Mortensen

这是基于 saeedgnu's version,但当前分支以星号和颜色显示,并且仅显示未描述为“月”或“年”之前的任何内容:

current_branch="$(git symbolic-ref --short -q HEAD)"
git for-each-ref --sort=committerdate refs/heads \
  --format='%(refname:short)|%(committerdate:relative)' \
  | grep -v '\(year\|month\)s\? ago' \
  | while IFS='|' read branch date
    do
      start='  '
      end=''
      if [[ $branch = $current_branch ]]; then
        start='* \e[32m'
        end='\e[0m'
      fi
      printf "$start%-30s %s$end\\n" "$branch" "$date"
    done

s
saeedgnu

我作为脚本的最佳结果:

git for-each-ref --sort=-committerdate refs/heads/ --format='%(refname:short)|%(committerdate:iso)|%(authorname)' |
    sed 's/refs\/heads\///g' |
    grep -v BACKUP  | 
    while IFS='|' read branch date author
    do 
        printf '%-15s %-30s %s\n' "$branch" "$date" "$author"
    done

什么样的剧本?重击?
P
Peter Mortensen

The accepted command-line answer 摇滚,但如果您想要更漂亮的东西,例如 GUI,并且您的来源 === “github”。

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


P
Peter Mortensen

最简单的与最后提交日期一起打印:

git branch --all  --format='%(committerdate:short) %(refname:short)'|sort

P
Peter Mortensen
git for-each-ref --sort=-committerdate refs/heads/

# Or using Git branch (since version 2.7.0)
git branch --sort=-committerdate  # Descending
git branch --sort=committerdate  # Ascending

一个解释将是有序的。例如,想法/要点是什么?它与之前的 30 个答案有何对比?来自 the Help Center“...总是解释为什么你提出的解决方案是合适的以及它是如何工作的”。请通过 editing (changing) your answer 回复,而不是在评论中(没有“编辑:”、“更新:”或类似的 - 答案应该看起来好像是今天写的)。
B
Ben

这是我正在寻找的变化:

git for-each-ref --sort=-committerdate --format='%(committerdate)%09%(refname:short)' refs/heads/ | tail -r

tail -r 反转列表,因此最近的 commiterdate 排在最后。


您还可以将 --sort=-committerdate 更改为 --sort=committerdate 来完成此操作。
哪个 tail-r
J
John Delaney

我将接受的答案的输出通过管道传输到 dialog,给我一个交互式列表:

#!/bin/bash

TMP_FILE=/tmp/selected-git-branch

eval `resize`
dialog --title "Recent Git Branches" --menu "Choose a branch" $LINES $COLUMNS $(( $LINES - 8 )) $(git for-each-ref --sort=-committerdate refs/heads/ --format='%(refname:short) %(committerdate:short)') 2> $TMP_FILE

if [ $? -eq 0 ]
then
    git checkout $(< $TMP_FILE)
fi

rm -f $TMP_FILE

clear

另存为(例如)~/bin/git_recent_branches.shchmod +x。然后 git config --global alias.rb '!git_recent_branches.sh' 给我一个新的 git rb 命令。


T
Tom

我知道已经有很多答案了,但这是我的两分钱一个简单的别名(我喜欢把我最近的分支放在底部):

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

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


C
ChunkCoder

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))' 这是您需要的