Joe*_*ite 1197 git version-control branch
我想得到一个Git存储库中所有分支的列表,顶部有"最新鲜"的分支,其中"最新鲜"的分支是最近提交的分支(因此,更可能是一个分支)我想要注意).
有没有办法可以使用Git(a)通过最新提交对分支列表进行排序,或者(b)以某种机器可读格式获取分支列表以及每个分支的最后提交日期?
最糟糕的情况是,我总是可以运行git branch以获取所有分支的列表,解析其输出,然后git log -n 1 branchname --format=format:%ci为每个分支获取每个分支的提交日期.但这将在Windows机器上运行,其中启动一个新进程相对昂贵,因此如果有很多分支,每个分支启动Git可执行文件可能会变慢.有没有办法用一个命令完成所有这些?
Jak*_*ski 1692
使用--sort=-committerdate选项git for-each-ref;
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
Run Code Online (Sandbox Code Playgroud)

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))'
Run Code Online (Sandbox Code Playgroud)

Bea*_*ith 118
扩展Jakub的答案和Joe的提示,以下将删除"refs/heads /",因此输出只显示分支名称:
git for-each-ref --count=30 --sort=-committerdate refs/heads/ --format='%(refname:short)'
Run Code Online (Sandbox Code Playgroud)
nik*_*lay 86
这是最佳代码,它结合了另外两个答案:
git for-each-ref --sort=-committerdate refs/heads/ --format='%(committerdate:short) %(authorname) %(refname:short)'
Run Code Online (Sandbox Code Playgroud)
use*_*406 78
这是一个简单的命令,列出了最新提交的所有分支:
git branch -v
Run Code Online (Sandbox Code Playgroud)
要按最近提交的顺序排序,请使用
git branch -v --sort=committerdate
Run Code Online (Sandbox Code Playgroud)
来源:http://git-scm.com/book/en/Git-Branching-Branch-Management
dim*_*mid 54
我使用以下别名:
recent = "!r(){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'|'}; r"
产生:

编辑:使用'|' 分开,感谢@BjörnLindqvist
更新:在当前分支之前添加*,感谢@elhadi
编辑:修复了当前分支是另一个分支的子字符串的情况
编辑:使用更简单的语法为当前分支,感谢@Joshua Skrzypek
est*_*ani 37
我还需要颜色,标签和远程参考,没有任何重复:
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]++'
Run Code Online (Sandbox Code Playgroud)
因为引用可能很难,这里是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'"]++'"
Run Code Online (Sandbox Code Playgroud)
And*_*ndy 36
我能够参考上面的例子来创造最适合我的东西.
git for-each-ref --sort = -committerdate refs/heads/--format ='%(authordate:short)%(color:red)%(objectname:short)%(color:yellow)%(refname:short )%(颜色:重置)(%(颜色:绿色)%(committerdate:relative)%(颜色:重置))'
pal*_*dot 25
我发现以下命令对我的目的很有帮助。
git branch --sort=-committerdate | head -n 10
Run Code Online (Sandbox Code Playgroud)
这将列出最新的 10 个分支。它很短,也可以在没有别名的情况下使用。
Joh*_*lor 24
其他答案似乎不允许传递-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
Run Code Online (Sandbox Code Playgroud)
如果您还想打印提交日期,则可以使用此版本:
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-
Run Code Online (Sandbox Code Playgroud)
样本输出:
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
Run Code Online (Sandbox Code Playgroud)
它可能更具可读性,分为多行:
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-
Run Code Online (Sandbox Code Playgroud)
这也应该与其他参数一起使用git branch,例如-vvr列出远程跟踪分支,或-vva列出远程跟踪和本地分支.
Von*_*onC 22
git 2.7(2015年第4季度)将直接引入分支排序git branch:
请参阅提交aa3bc55,提交aedcb7d,提交1511b22,提交f65f139,...(2015年9月23日),提交aedcb7d,提交1511b22,提交ca41799(2015年9月24日),以及提交f65f139,...(2015年9月23日)作者:Karthik Nayak(KarthikNayak).
(由Junio C gitsterHamano合并- -在提交7f11b48,2015年10月15日)
特别是,提交aedcb7d:
branch.c:使用'ref-filter'API
制作" branch.c"使用" ref-filter"的API,通过裁判排序迭代.这删除了' branch.c'用ref-filter'库'调用替换它时使用的大部分代码.
根据给定的密钥排序.
前缀-按值的降序排序.您可以
--sort=<key>多次使用该选项,在这种情况下,最后一个键成为主键.支持的密钥与中
git for-each-ref的密钥相同.
排序顺序默认为基于完整引用名称(包括refs/...前缀)的排序.这首先列出分离的HEAD(如果存在),然后是本地分支,最后是远程跟踪分支.
这里:
git branch --sort=-committerdate
Run Code Online (Sandbox Code Playgroud)
或者(见下面的Git 2.19)
# if you are sure to /always/ want to see branches ordered by commits:
git config --global branch.sort -committerdate
git branch
Run Code Online (Sandbox Code Playgroud)
另见Karthik Nayak()提交9e46833(2015年10月30日).
帮助:Junio C Hamano().(通过合并JUNIOÇ滨野- -在提交415095f,03 2015年11月)KarthikNayakgitster
gitster
当按照数值(例如
--sort=objectsize)进行排序时,当两个ref保持相同的值时,没有回退比较.如Johannes Sixt($ gmane/280117)所指出的,这可能会导致意外的结果(即无法预先确定具有相同值的列表引用的顺序).因此,只要其他标准相等,就根据refname回退到字母比较.
$ git branch --sort=objectsize
* (HEAD detached from fromtag)
branch-two
branch-one
master
Run Code Online (Sandbox Code Playgroud)
使用Git 2.19,可以默认设置排序顺序.
git branch支持配置branch.sort,比如git tag已有配置tag.sort.
见Samuel Maftoul 提交的560ae1c(2018年8月16日)(``).
(由Junio C gitsterHamano合并- -在提交d89db6f,2018年8月27日)
Run Code Online (Sandbox Code Playgroud)branch.sort:此变量控制显示时分支的排序顺序
git-branch.
如果未--sort=<value>提供" "选项,则此变量的值将用作默认值.
要列出远程分支,请使用git branch -r --sort=objectsize.该-r标志使其列出远程分支而不是本地分支.
n8t*_*8tr 19
我喜欢使用相对日期并缩短分支名称,如下所示:
git for-each-ref --sort='-authordate:iso8601' --format=' %(authordate:relative)%09%(refname:short)' refs/heads
Run Code Online (Sandbox Code Playgroud)
哪个给你输出:
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
Run Code Online (Sandbox Code Playgroud)
我建议制作一个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 ' ------------------------------------------------------------'"
Run Code Online (Sandbox Code Playgroud)
然后你可以这样做以获得一个格式良好和排序的本地分支列表:
git branches
Run Code Online (Sandbox Code Playgroud)
更新:如果您想要着色,请执行此操作:
#!/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)$|$"
Run Code Online (Sandbox Code Playgroud)
use*_*247 14
从git 2.19开始,您可以简单地:
git branch --sort=-committerdate
Run Code Online (Sandbox Code Playgroud)
你也可以:
git config branch.sort -committerdate
Run Code Online (Sandbox Code Playgroud)
因此,无论何时在当前存储库中列出分支,它都将按committerdate排序.
如果每次列出分支时,都希望它们按comitterdate排序:
git config --global branch.sort -committerdate
Run Code Online (Sandbox Code Playgroud)
免责声明:我是git中这个功能的作者,我在看到这个问题时实现了它.
epy*_*nkn 11
添加一些颜色(因为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"
Run Code Online (Sandbox Code Playgroud)
hIp*_*pPy 10
Git v2.19引入了branch.sort配置选项(请参阅branch.sort)。
因此git branch默认情况下将按提交者日期(降序)排序
# gitconfig
[branch]
sort = -committerdate # Descending
Run Code Online (Sandbox Code Playgroud)
脚本:
git config --global branch.sort -committerdate
Run Code Online (Sandbox Code Playgroud)
所以,
git branch
Run Code Online (Sandbox Code Playgroud)
输出:
* dev
master
_
Run Code Online (Sandbox Code Playgroud)
和
git branch -v
Run Code Online (Sandbox Code Playgroud)
输出:
* dev 0afecf5 Merge branch 'oc' into dev
master 652428a Merge branch 'dev'
_ 7159cf9 Merge branch 'bashrc' into dev
Run Code Online (Sandbox Code Playgroud)
我有同样的问题,所以我写了一个名为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
Run Code Online (Sandbox Code Playgroud)
它还允许您存储每个分支的自定义属性,例如,票证ID,状态,待办事项,并根据这些属性过滤分支列表.更多信息:http://rondevera.github.io/twig/
我想出了以下命令(适用于Git 2.13及更高版本):
git branch -r --sort=creatordate \
--format "%(creatordate:relative);%(committername);%(refname:lstrip=-1)" \
| grep -v ";HEAD$" \
| column -s ";" -t
Run Code Online (Sandbox Code Playgroud)
如果没有column,可以用最后一行替换
| sed -e "s/;/\t/g"
Run Code Online (Sandbox Code Playgroud)
输出看起来像
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
Run Code Online (Sandbox Code Playgroud)
仅供参考,如果您想获得最近签出的分支列表(而不是最近提交的分支),您可以使用git的reflog:
$ git reflog | egrep -io "moving from ([^[:space:]]+)" | awk '{ print $3 }' | head -n5
master
stable
master
some-cool-feature
feature/improve-everything
Run Code Online (Sandbox Code Playgroud)
另请参阅:如何获取我最近检出的git分支列表?
这是另一个脚本,它执行所有其他脚本的操作。事实上,它为你的 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}"
}
Run Code Online (Sandbox Code Playgroud)
小智 5
这是我用来在最近的分支之间切换的一个小脚本:
#!/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
Run Code Online (Sandbox Code Playgroud)
使用这两个别名:
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)'
Run Code Online (Sandbox Code Playgroud)
只需在 Git 存储库中调用它,它就会显示最后 N 个分支(默认为 10 个)以及每个分支旁边的数字。输入分支的编号,它会检查:
通常我们最近会考虑远程分支。所以试试这个
git fetch
git for-each-ref --sort=-committerdate refs/remotes/origin
Run Code Online (Sandbox Code Playgroud)
另一种变体:
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'|'
Run Code Online (Sandbox Code Playgroud)
值得注意的是,即使它正在查看远程分支中的更改,也值得在运行命令之前与 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
351596 次 |
| 最近记录: |