如何获取按最近提交排序的 Git 分支列表? [英] How can I get a list of Git branches, ordered by most recent commit?

查看:28
本文介绍了如何获取按最近提交排序的 Git 分支列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取 Git 存储库中所有分支的列表,其中最新鲜"的分支位于顶部,其中最新鲜"的分支是最近提交的分支(因此更有可能成为我想关注的人).

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

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

解决方案

使用

I want to get a list of all the branches in a Git repository with the "freshest" branches at the top, where the "freshest" branch is the one that's been committed to most recently (and is, therefore, more likely to be one I want to pay attention to).

Is there a way I can use Git to either (a) sort the list of branches by latest commit, or (b) get a list of branches together with each one's last-commit date, in some kind of machine-readable format?

Worst case, I could always run git branch to get a list of all the branches, parse its output, and then git log -n 1 branchname --format=format:%ci for each one, to get each branch's commit date. But this will run on a Windows box, where spinning up a new process is relatively expensive, so launching the Git executable once per branch could get slow if there are a lot of branches. Is there a way to do all this with a single command?

解决方案

Use the --sort=-committerdate option of git for-each-ref;

Also available since Git 2.7.0 for git branch:

Basic Usage:

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

Result:

Advanced Usage:

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

Result:

Pro Usage (Unix):

You can put the following snippet in your ~/.gitconfig. The recentb alias accepts two arguments:

  • refbranch: which branch the ahead and behind columns are calculated against. Default master
  • count: how many recent branches to show. Default 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"

Result:

这篇关于如何获取按最近提交排序的 Git 分支列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆