如何获得按最近提交排序的Git分支列表?
我想得到一个Git存储库中所有分支的列表,顶部是“最新”的分支,其中“最新”的分支是最近承诺的分支(因此,更可能是我想要关注的分支) 有没有一种方法可以使用Git(a)按最新提交对分支列表进行排序,或者(b)以某种机器可读的格式获取分支列表以及每个分支的最后提交日期 最糟糕的情况是,我总是可以运行如何获得按最近提交排序的Git分支列表?,git,version-control,branch,Git,Version Control,Branch,我想得到一个Git存储库中所有分支的列表,顶部是“最新”的分支,其中“最新”的分支是最近承诺的分支(因此,更可能是我想要关注的分支) 有没有一种方法可以使用Git(a)按最新提交对分支列表进行排序,或者(b)以某种机器可读的格式获取分支列表以及每个分支的最后提交日期 最糟糕的情况是,我总是可以运行git branch来获取所有分支的列表,解析其输出,然后对每个分支运行git log-n 1 branchname--format=format:%ci,以获取每个分支的提交日期。但这将在Window
git branch
来获取所有分支的列表,解析其输出,然后对每个分支运行git log-n 1 branchname--format=format:%ci
,以获取每个分支的提交日期。但这将在Windows机器上运行,在Windows机器上,启动一个新进程的成本相对较高,因此,如果有很多分支,则每个分支启动一次Git可执行文件可能会变慢。有没有一种方法可以用一个命令来完成这一切?使用的--sort=-committerdate
选项
也可用于:
基本用法:
git for each ref--sort=-committerdate refs/heads/
#或使用git分支(自版本2.7.0起)
git分支——sort=-committerdate#DESC
git分支——sort=committerdate#ASC
结果:
高级用法:
git for each ref--sort=committerdate refs/heads/--format='%(HEAD)%(颜色:黄色)%(refname:short)%(颜色:重置)-(objectname:short)%(颜色:重置)-(contents:subject)-(authorname)((颜色:绿色)%(committerdate:relative)%(颜色:重置))'
结果:
Pro使用(Unix):
您可以将以下代码片段放入~/.gitconfig
中。recentb别名接受两个参数:
:根据哪个分支计算前后列。默认主控refbranch
:要显示的最近分支数。默认值20计数
[别名]
#注意:所有别名的前缀都是!在/bin/sh中运行确保使用sh语法,而不是bash/zsh或其他任何语法
recentb=“!r(){refbranch=$1 count=$2;git for each ref--sort=-committerdate refs/heads--format='%(refname:short)|%(HEAD)%(color:yellow)%%(refname:short)|%(color:bled green)%%(committerdate:relative)%.(subject)|%(color:magenta)%%(authorname)%%(color:reset)--color=always--color=读取回显行时计数:${20};do branch10月1日,TRD-d’’’;前方=美元(git-rev名单——计数计数(git-rev名单——计数计数(git-rev名单——计数(git-rev名单——2007 2007 2007年10月10月10日线路)10月10日当天当天当天当天当天当天当天当天当天当天当天当天当天当天当天当天当天当天当天当天(git-rev名单——线路——据据据据据据据据据据据据录录录录录录录录录录(git(git-rev名单——名单——据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据据(5,1,70美元)1';完成(回声“前面|后面|分支|最后提交|消息|作者\\n\“&&cat)|列-ts'|'};r”
结果:
按最近提交顺序排列的Git分支名称列表…
在和上展开,以下内容将去掉“refs/heads/”,因此输出仅显示分支名称:
命令:
结果:
以下是最佳代码,它结合了其他两个答案:
git for-each-ref --sort=-committerdate refs/heads/ --format='%(committerdate:short) %(authorname) %(refname:short)'
下面是一个简单的命令,列出了具有最新提交的所有分支:
git branch -v
要按最近的提交进行排序,请使用
git branch -v --sort=committerdate
来源:我也有同样的问题,所以我写了一个名为。它按时间顺序列出分支(最新优先),还可以让您设置最长期限,这样您就不会列出所有分支(如果您有很多分支的话)。例如:
$ 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、状态、TODO,并根据这些属性筛选分支列表。更多信息:我的最佳脚本结果:
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
这是基于,但当前分支以星形和彩色显示,仅显示未在“月”或“年”之前描述的任何内容:
current_branch=“$(git symbolic ref--short-q HEAD)”
每个ref的git--sort=committerdate refs/heads\
--格式='%(refname:short)|%(committerdate:relative)'\
|grep-v'(年、月)s\?阿戈\
|而IFS=“|”读取分支日期
做
开始=“”
结束=“”
如果[[$branch=$current_branch]];然后
开始='*\e[32m'
结束='\e[0m'
fi
printf“$start%-30s%s$end\\n”“$branch”“$date”
完成
我还需要颜色、标签和没有任何副本的远程引用:
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]++'
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(黄色)%d%Creset%C(粗体蓝色)%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'"]++'"
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(黄色)%d%Creset%C(蓝色粗体)%Creset%n”| cat;done | awk'!a[“$0'”++”
其他答案似乎不允许传递-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“完成”|排序-r |剪切-f2
如果您还想打印提交日期,可以改用此版本:
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)$REPLY“完成”|排序-r |剪切-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
#下划线是因为活动分支为pr
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
[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 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 for-each-ref --sort=-committerdate --format='%(committerdate)%09%(refname:short)' refs/heads/ | tail -r
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
#!/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)$|$"
$ git reflog | egrep -io "moving from ([^[:space:]]+)" | awk '{ print $3 }' | head -n5
master
stable
master
some-cool-feature
feature/improve-everything
git branch --sort=-committerdate
# if you are sure to /always/ want to see branches ordered by commits:
git config --global branch.sort -committerdate
git branch
$ git branch --sort=objectsize
* (HEAD detached from fromtag)
branch-two
branch-one
master
branch.sort:
#!/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 branch -r --sort=creatordate \
--format "%(creatordate:relative);%(committername);%(refname:lstrip=-1)" \
| grep -v ";HEAD$" \
| column -s ";" -t
| 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
git branch --sort=-committerdate
git config branch.sort -committerdate
git config --global branch.sort -committerdate
[alias]
br = !git branch --sort=committerdate --color=always | tail -n15
[color "branch"]
current = yellow
local = cyan
remote = red
git fetch
git for-each-ref --sort=-committerdate refs/remotes/origin
alias gb='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))'"'"''
git branch --all --format='%(committerdate:short) %(refname:short)'|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'|'
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
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