我想获取 Git 存储库中所有分支的列表,顶部是“最新”分支,其中“最新”分支是最近提交的分支(因此,更有可能是一个我要注意)。
有没有一种方法可以使用 Git (a) 按最新提交对分支列表进行排序,或者 (b) 以某种机器可读的格式获取分支列表以及每个分支的最后提交日期?
最坏的情况是,我总是可以运行 git branch
来获取所有分支的列表,解析其输出,然后为每个分支运行 git log -n 1 branchname --format=format:%ci
,以获取每个分支的提交日期。但这将在 Windows 机器上运行,其中启动一个新进程相对昂贵,因此如果有很多分支,每个分支启动一次 Git 可执行文件可能会变慢。有没有办法用一个命令来完成这一切?
git for-each-ref
的答案:您可以让远程分支通过 refs/remotes/
而不是 refs/heads/
(或者您可以同时通过两者,空格分隔); refs/tags/
用于标记,或者仅 refs/
用于所有三种。
使用 git for-each-ref
的 --sort=-committerdate
选项;
也可用于 git branch
的 since 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
Git 分支名称列表,按最近提交排序...
在 Jakub’s answer 和 Joe’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
。
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))'
git branch -va --sort=committerdate | tail -5
。也许这是您所询问和发现的替代方案。
这是一个简单的命令,列出了所有最新提交的分支:
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)'
将包括日期。
这是最佳代码,它结合了其他两个答案:
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)'
我使用以下别名:
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)。
'%(HEAD) ...'
即可获得相同的效果,而无需通过 sed
命令进行管道传输
[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'|'
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'|'}
!r(){git for-each-ref ... ;}; r
-ts'|'
之后添加一个分号,即 "!r(){ git ... -ts'|'; }; r"
我能够参考前面的示例来创建最适合我的东西。
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)"'
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))'
git 2.7(2015 年第四季度)将直接使用 git branch
引入分支排序:
请参阅 commit aa3bc55、commit aedcb7d、commit 1511b22、commit 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 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 7c5045f、commit 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 4045f65、commit 2708ce6、commit 7c269a7、commit d094748、commit 75c50e5(2021 年 1 月 7 日)和 commit 08bf6a8、commit 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
使用 Git 2.35(2022 年第一季度),类似“git -c branch.sort=bogus branch new HEAD
”(man),即
“git branch
”(man) 的操作模式 命令不需要排序键信息,不再因看到 伪造的排序键而出错。
请参阅 Junio C Hamano (gitster
) 的 commit 98e7ab6、commit 1a89796(2021 年 10 月 20 日)。
(由 Junio C Hamano -- gitster
-- 在 commit 5126145 中合并,2021 年 11 月 29 日)
for-each-ref: 延迟解析 --sort=
for-each-ref 系列命令在看到每个 --sort=
-r
我还需要没有任何重复的颜色、标签和远程引用:
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'"]++'"
| head -n20
。如果您使用的是别名,请确保它在 in 引号内。
从 Git 2.19 开始,您可以简单地:
git branch --sort=-committerdate
你也可以:
git config branch.sort -committerdate
因此,每当您列出当前存储库中的分支时,它将按提交日期排序。
如果每当您列出分支时,您希望它们按提交日期排序:
git config --global branch.sort -committerdate
免责声明:我是 Git 中这个功能的作者,我在看到这个问题时实现了它。
其他答案似乎不允许传递 -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
相同。
-v, -vv, --verbose
部分包含以下内容:If given twice, print the name of the upstream branch, as well
我喜欢使用相对日期并像这样缩短分支名称:
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)$|$"
我发现以下命令对我的目的很有帮助。
git branch --sort=-committerdate | head -n 10
这将列出最新的 10 个分支。它很短,也可以在没有别名的情况下使用。
git config --global alias.br "! git branch --sort=-committerdate | head -n 10"
添加一些颜色(因为 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"
我想出了以下命令(适用于 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 关于各个部分的工作原理。
git branch --sort
。
另一种变化:
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 cmd 和 PowerShell 的版本:
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
我遇到了同样的问题,所以我编写了一个名为 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/
仅供参考,如果您想获得最近签出的分支列表(而不是最近提交的),您可以使用 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?
这是我用来在最近的分支之间切换的小脚本:
#!/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
通常我们最近会考虑远程分支。所以试试这个
git fetch
git for-each-ref --sort=-committerdate refs/remotes/origin
这是另一个脚本,它执行所有其他脚本所做的事情。实际上,它为您的 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}"
}
仅获取根据提交者日期排序的前五个分支名称:
git branch --sort=-committerdate | head -5
这是基于 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
我作为脚本的最佳结果:
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
The accepted command-line answer 摇滚,但如果您想要更漂亮的东西,例如 GUI,并且您的来源 === “github”。
您可以单击存储库中的“分支”。或直接点击网址:https://github.com/ORGANIZATION_NAME/REPO_NAME/branches
最简单的与最后提交日期一起打印:
git branch --all --format='%(committerdate:short) %(refname:short)'|sort
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
这是我正在寻找的变化:
git for-each-ref --sort=-committerdate --format='%(committerdate)%09%(refname:short)' refs/heads/ | tail -r
tail -r
反转列表,因此最近的 commiterdate
排在最后。
tail
有 -r
?
我将接受的答案的输出通过管道传输到 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.sh
和 chmod +x
。然后 git config --global alias.rb '!git_recent_branches.sh'
给我一个新的 git rb
命令。
我知道已经有很多答案了,但这是我的两分钱一个简单的别名(我喜欢把我最近的分支放在底部):
[alias]
br = !git branch --sort=committerdate --color=always | tail -n15
[color "branch"]
current = yellow
local = cyan
remote = red
这将为您提供最新的 15 个分支的概览,以颜色显示,并突出显示您当前的分支(并且它有一个星号)。
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))'
这是您需要的
不定期副业成功案例分享
--format=%(refname)
将输出限制为仅引用名称。git for-each-ref --sort=-committerdate refs/heads/ --format='%(refname) %(committerdate) %(authorname)' | sed 's/refs\/heads\///g'
:shortname
?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))'