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?
FYI, if you'd like to get a list of recently checked out branches (as opposed to recently committed) you can use git's reflog:
See also: How can I get a list of git branches that I've recently checked out?
I had the same problem, so I wrote a Ruby gem called Twig. It lists branches in chronological order (newest first), and can also let you set a max age so that you don't list all branches (if you have a lot of them). For example:
It also lets you store custom properties for each branch, e.g., ticket id, status, todos, and filter the list of branches according to these properties. More info: http://rondevera.github.io/twig/
Here's a little script that I use to switch between recent branchs:
Using those two aliases
Just call that in a git repo and it will show you the last N branches (10 by default) and a number aside each. Input the number of the branch and it checks out:
git 2.7 (Q4 2015) will introduce branch sorting using directly
git branch
:See commit aa3bc55, commit aedcb7d, commit 1511b22, commit f65f139, ... (23 Sep 2015), commit aedcb7d, commit 1511b22, commit ca41799 (24 Sep 2015), and commit f65f139, ... (23 Sep 2015) by Karthik Nayak (
KarthikNayak
).(Merged by Junio C Hamano --
gitster
-- in commit 7f11b48, 15 Oct 2015)In particular, commit aedcb7d:
Make '
branch.c
' use 'ref-filter
' APIs for iterating through refs sorting. This removes most of the code used in 'branch.c
' replacing it with calls to the 'ref-filter
' library.It adds the option
--sort=<key>
:Here:
Or (see below with Git 2.19)
See also commit 9e46833 (30 Oct 2015) by Karthik Nayak (
KarthikNayak
).Helped-by: Junio C Hamano (
gitster
).(Merged by Junio C Hamano --
gitster
-- in commit 415095f, 03 Nov 2015)With Git 2.19, the sort order can be set by default.
git branch
supports a configbranch.sort
, likegit tag
, which already had a configtag.sort
.See commit 560ae1c (16 Aug 2018) by Samuel Maftoul (``).
(Merged by Junio C Hamano --
gitster
-- in commit d89db6f, 27 Aug 2018)To list remote branches, use
git branch -r --sort=objectsize
. The-r
flag causes it to list remote branches instead of local branches.I pipe the output from the accepted answer into
dialog
, to give me an interactive list:Save as (e.g.)
~/bin/git_recent_branches.sh
andchmod +x
it. Thengit config --global alias.rb '!git_recent_branches.sh'
to give me a newgit rb
command.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))'
this is that you need