How can I get a list of git branches, ordered by most recent commit?

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:

结果


To expand on Jakub's answer and Joe's tip, the following will strip out the "refs/heads/" so the output only displays the branch names:

git for-each-ref --count=30 --sort=-committerdate refs/heads/ --format='%(refname:short)'

最近的git分支


这是最佳代码,它结合了另外两个答案:

git for-each-ref --sort=-committerdate refs/heads/ --format='%(committerdate:short) %(authorname) %(refname:short)'
链接地址: http://www.djcxy.com/p/4520.html

上一篇: 如何在Python中制作二维数组的副本?

下一篇: 我怎样才能得到最近提交排序的git分支列表?