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

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

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


当前回答

我使用以下别名:

最近=“!r(){count=$1;每个ref的git--sort=-committedate-refs/heads--format='%(HEAD)%(颜色:黄色)%(refname:short)|%(颜色∶粗体绿色)%(committedate:relative)|%

其产生:

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

git最近20(默认值为10)。

其他回答

我也有同样的问题,所以我写了一个Ruby宝石,叫做Twig。它按时间顺序列出分支(最新的第一个),还可以让您设置最大年龄,这样您就不会列出所有分支(如果您有很多分支)。例如:

$ 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、状态、todos,并根据这些财产筛选分支列表。更多信息:http://rondevera.github.io/twig/

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

git for each ref--sort=-提交日期refs/heads--format='%(authordate:短)%(颜色:红色)%(对象名称:短)%(%(颜色:绿色)%(提交日期:相对)%(颜色重置))'

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

git for each ref--sort=-committedate refs/heads refs/remotes--format='%(authordate:短)%(颜色:红色)%(对象名称:短)%(%(颜色:绿色)%(提交日期:相对)%(颜色重置))%(作者姓名)'

下面是作为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 branch --sort=-committerdate | head -n 10

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

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

所以这里有一个单行程序,按照提交日期、保存颜色等对gitbranch-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分支的其他参数一起使用,例如-vvr列出远程跟踪分支,或-vva列出远程跟踪和本地分支。

下面是我用来在最近的分支之间切换的一个小脚本:

#!/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个)和每个分支旁边的数字。输入分支机构的编号,它将签出: