我怎样才能得到最近提交排序的git分支列表? [英] How can I get a list of git branches, ordered by most recent commit?

查看:177
本文介绍了我怎样才能得到最近提交排序的git分支列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得Git仓库中所有分支的列表,其中最新鲜的分支位于顶部,其中最新鲜分支是最近被承诺的分支(并且因此更可能我可以使用Git来(a)通过最新的提交对分支列表进行排序,或者( b)以某种机器可读的格式获取分支的列表以及每个分支的最后提交日期?

最糟糕的情况是,我总是可以运行 git branch 得到所有分支的列表,解析它的输出,然后 git log -n 1 branchname --format = format:%ci 来获取每个分支的提交日期。但是这会在Windows机器上运行,在这个机器上启动一个新的进程相对比较昂贵,所以如果有很多分支机构,每个分支启动一次git可执行文件就会变慢。有没有一种方法可以用一个命令完成所有这些工作?使用 - 使用 sort = -committerdate 选项 git for-each-ref ;
也可用自Git 2.7.0 git branch


基本用法:



  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)%(color:yellow)%(refname :%(颜色:重置) - %(颜色:红色)%(对象名称:短)%(颜色:重置) - %(内容:主题) - %(作者名)(%(颜色:绿色)%(提交日期:相对)%(color:reset))'



结果:




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 --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:

这篇关于我怎样才能得到最近提交排序的git分支列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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