2040

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?

10
  • 3
    stackoverflow.com/a/2514279/1804124 Has a better answer.
    – Spundun
    Commented Jan 25, 2013 at 3:36
  • 18
    @Spundun, you lost me there. How is a combination of multiple commands, including stuff piped through perl and sed, "better" than using a command that Git already has?
    – Joe White
    Commented Jan 25, 2013 at 3:40
  • 1
    Because with the answer here , I didn't get all the branches in the repo. In my particular case, the answer would give me one branch and the answer there gave me 20 or so branches(with the -r option).
    – Spundun
    Commented Jan 25, 2013 at 18:18
  • 51
    @Spundun regarding the answer with git for-each-ref from Jakub Narębski: you can get remote branches passing refs/remotes/ instead of refs/heads/ (or you can pass both, whitespace-separated); refs/tags/ for tags, or just refs/ for all three kinds.
    – jakub.g
    Commented Jan 27, 2013 at 4:45
  • 10
    Starting git 2.7 (Q4 2015), no more for-each-ref! You will use directly git branch --sort=-committerdate: see my answer below
    – VonC
    Commented Oct 16, 2015 at 5:58

32 Answers 32

2857

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:

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:

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:

Recentb alias result

38
  • 17
    Perfect! I can even restrict the output to just the ref names by appending --format=%(refname).
    – Joe White
    Commented Mar 4, 2011 at 12:45
  • 42
    This is better for me: git for-each-ref --sort=-committerdate refs/heads/ --format='%(refname) %(committerdate) %(authorname)' | sed 's/refs\/heads\///g'
    – saeedgnu
    Commented Jan 30, 2012 at 10:01
  • 3
    @ilius: why not use :shortname? Commented Feb 5, 2012 at 21:45
  • 38
    @ilius: As @BeauSmith wrote: git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads/. git-for-each-ref(1) manpage says: For a non-ambiguous short name of the ref append :short. Commented Feb 6, 2012 at 10:21
  • 94
    This is a colorized version including hashes, messages, ordered ascending based on commit date, with the relative age of the last commit on each branch. I stole all of the ideas from you guys above. It's in my .gitconfig in the [alias] section and I love it. br = 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))' Commented Mar 13, 2015 at 22:01
177

Here is a simple command that lists all branches with latest commits:

git branch -v

To order by most recent commit, use

git branch -v --sort=committerdate

Source: http://git-scm.com/book/en/Git-Branching-Branch-Management

7
  • 21
    git branch -av if you want to see non-local branches too. Commented Jan 18, 2014 at 3:59
  • 8
    Is it easy to get git branch -v to include the date of each commit listed?
    – dumbledad
    Commented Oct 13, 2015 at 11:12
  • 12
    This is awesome. I like git branch -va --sort=-committerdate to show non-local branches, with the most recently changed branches at the top. Commented Apr 11, 2018 at 16:37
  • 7
    git branch -v --format='%(committerdate:short) %(refname:short)' will include the date.
    – Teepeemm
    Commented Jun 16, 2022 at 23:05
  • For those coming from search, yes, committerdate will catch the last time that you amended or rebased. (The other possibility is authordate, which amend or rebase will not change by default.) Commented Dec 27, 2022 at 15:17
164

List of Git branch names, ordered by most recent commit…

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


Command:

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

Result:

Recent Git branches

6
  • 9
    Is there any way to do this for the REMOTE repository?
    – Allan Bowe
    Commented Jun 24, 2014 at 14:50
  • 15
    aah - @jakub.g already explained: you can get remote branches passing refs/remotes/ instead of refs/heads/. Perfect!!
    – Allan Bowe
    Commented Jun 24, 2014 at 14:52
  • I like this one, if you're trying to alias it, which I did, to git rb remember to surround the command in quotes: git config --global alias.rb "for-each-ref --count=20 --sort=-committerdate refs/heads/ --format=\'%(refname:short)\'" Commented Feb 27, 2015 at 16:49
  • 7
    And now you can do this with git branch, so getting local, remote or all branches works like on git-branch (i.e. -r, -a). git branch -r --sort=committerdate --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(color:red)%(objectname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))'
    – Phil Hord
    Commented Mar 16, 2017 at 16:58
  • 1
    @AllanBowe the following will output the top 5 active branches in a repo: git branch -va --sort=committerdate | tail -5. Perhaps that's an alternative to what you were asking and discovered.
    – marckassay
    Commented Jul 21, 2020 at 14:04
105

Here's the optimal code, which combines the other two answers:

git for-each-ref --sort=-committerdate refs/heads/ --format='%(committerdate:short) %(authorname) %(refname:short)'
4
  • 11
    Even a little mor optimized to get a tabular output: git for-each-ref --sort=-committerdate refs/heads/ --format='%(committerdate:short) %(authorname) %(refname:short)'
    – schoetbi
    Commented Mar 1, 2013 at 12:58
  • 5
    for some reason i had to use double-quotes on windows, but otherwise these work just fine :)
    – amenthes
    Commented Feb 25, 2016 at 13:35
  • 1
    @schoetbi That code looks exactly like the one from nikolay, what did you change to make it tabular?
    – Enrico
    Commented Mar 22, 2016 at 13:33
  • 1
    @Enrico and others that may wonder about the same thing. nikolay changed his answer using schoetbis suggestion. By moving date first which is always the same length the result seems more tabular.
    – johnny
    Commented Feb 11, 2020 at 7:21
101

I use the following alias:

recent = "!r() { count=$1; git for-each-ref --sort=-committerdate refs/heads --format='%(HEAD)%(color:yellow)%(refname:short)|%(color:bold green)%(committerdate:relative)|%(color:blue)%(subject)|%(color:magenta)%(authorname)%(color:reset)' --color=always --count=${count:=10} | column -ts'|';}; r"

which produces:

Result

We can also give a custom count, e.g.,

git recent 20 (the default is 10).

19
  • 4
    At least in the latest version of git you can just add '%(HEAD) ...' at the start of the format string to get the same effect without piping throught the sed command Commented Sep 19, 2016 at 11:15
  • 6
    I could not git this to work as a git alias. I had to use [alias] recent = !git for-each-ref --sort=-committerdate refs/heads --format='%(HEAD)%(color:yellow)%(refname:short)|%(color:bold green)%(committerdate:relative)|%(color:blue)%(subject)|%(color:magenta)%(authorname)%(color:reset)'|column -ts'|' Commented Oct 6, 2016 at 11:44
  • 16
    I had to add --color=always to get color. git for-each-ref --sort=-committerdate refs/heads --format='%(HEAD)%(color:yellow)%(refname:short)|%(color:bold green)%(committerdate:relative)|%(color:blue)%(subject)|%(color:magenta)%(authorname)%(color:reset)' --color=always|column -ts'|'}
    – JohnFlux
    Commented Jun 19, 2018 at 21:29
  • 5
    @mwfearnley: For me, putting a semicolon inside the braces helped !r(){git for-each-ref ... ;}; r Commented Jun 27, 2019 at 12:34
  • 4
    I also need to add a semi-colon after -ts'|' i.e. "!r(){ git ... -ts'|'; }; r"
    – Emmanuel
    Commented Oct 18, 2019 at 2:15
90

I was able to reference the previous examples to create something that works best for me.

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

Screenshot of Output

As suggested in the comments below you can also include remote branches and the author's name.

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

Screenshot of Output

Here are both commands as shell aliases that you can easily add to your shell profile.

# 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)"'
6
  • 1
    That looks nice, more colorful than using directly git branch as I suggested in stackoverflow.com/a/33163401/6309. +1
    – VonC
    Commented Jun 13, 2017 at 19:25
  • Thanks, @VonC glad you like it!
    – Andrew
    Commented Jun 13, 2017 at 20:47
  • 1
    Ditto - this one worked out-of-box, unlike a number of others I just tried. Thanks. Commented Nov 18, 2017 at 23:56
  • 12
    This one is clean and neat. Sometime I'd add remote and authorname as this: git for-each-ref --sort=-committerdate refs/heads/ refs/remotes --format='%(authordate:short) %(authorname) %(color:red)%(objectname:short) %(color:yellow)%(refname:short)%(color:reset) (%(color:green)%(committerdate:relative)%(color:reset))'
    – ywu
    Commented Oct 21, 2019 at 15:14
  • 1
    Like this one! Clean and concise, thanks. :)
    – dmitryb
    Commented Feb 2, 2022 at 10:51
47

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:

branch.c: use 'ref-filter' APIs

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

Sort based on the key given.
Prefix - to sort in descending order of the value.

You may use the --sort=<key> option multiple times, in which case the last key becomes the primary key.

The keys supported are the same as those in git for-each-ref.
Sort order defaults to sorting based on the full refname (including refs/... prefix). This lists detached HEAD (if present) first, then local branches and finally remote-tracking branches.

Here:

git branch --sort=-committerdate 

Or (see below with Git 2.19)

# if you are sure to /always/ want to see branches ordered by commits:
git config --global branch.sort -committerdate
git branch

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)

When sorting as per numerical values (e.g. --sort=objectsize) there is no fallback comparison when both refs hold the same value. This can cause unexpected results (i.e. the order of listing refs with equal values cannot be pre-determined) as pointed out by Johannes Sixt ($gmane/280117).

Hence, fallback to alphabetical comparison based on the refname whenever the other criterion is equal.

$ git branch --sort=objectsize

*  (HEAD detached from fromtag)
    branch-two
    branch-one
    master

With Git 2.19, the sort order can be set by default.
git branch supports a config branch.sort, like git tag, which already had a config tag.sort.
See commit 560ae1c (16 Aug 2018) by Samuel Maftoul (``).
(Merged by Junio C Hamano -- gitster -- in commit d89db6f, 27 Aug 2018)

branch.sort:

This variable controls the sort ordering of branches when displayed by git-branch.
Without the "--sort=<value>" option provided, the value of this variable will be used as the default.


To list remote branches, use git branch -r --sort=objectsize. The -r flag causes it to list remote branches instead of local branches.


With Git 2.27 (Q2 2020), "git branch" and other "for-each-ref" variants accepted multiple --sort=<key> options in the increasing order of precedence, but it had a few breakages around "--ignore-case" handling, and tie-breaking with the refname, which have been fixed.

See commit 7c5045f, commit 76f9e56 (03 May 2020) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit 6de1630, 08 May 2020)

ref-filter: apply fallback refname sort only after all user sorts

Signed-off-by: Jeff King

Commit 9e468334b4 ("ref-filter: fallback on alphabetical comparison", 2015-10-30, Git v2.7.0-rc0 -- merge listed in batch #10) taught ref-filter's sort to fallback to comparing refnames.
But it did it at the wrong level, overriding the comparison result for a single "--sort" key from the user, rather than after all sort keys have been exhausted.

This worked correctly for a single "--sort" option, but not for multiple ones.
We'd break any ties in the first key with the refname and never evaluate the second key at all.

To make matters even more interesting, we only applied this fallback sometimes!
For a field like "taggeremail" which requires a string comparison, we'd truly return the result of strcmp(), even if it was 0.
But for numerical "value" fields like "taggerdate", we did apply the fallback. And that's why our multiple-sort test missed this: it uses taggeremail as the main comparison.

So let's start by adding a much more rigorous test. We'll have a set of commits expressing every combination of two tagger emails, dates, and refnames. Then we can confirm that our sort is applied with the correct precedence, and we'll be hitting both the string and value comparators.

That does show the bug, and the fix is simple: moving the fallback to the outer compare_refs() function, after all ref_sorting keys have been exhausted.

Note that in the outer function we don't have an "ignore_case" flag, as it's part of each individual ref_sorting element. It's debatable what such a fallback should do, since we didn't use the user's keys to match.
But until now we have been trying to respect that flag, so the least-invasive thing is to try to continue to do so.
Since all callers in the current code either set the flag for all keys or for none, we can just pull the flag from the first key. In a hypothetical world where the user really can flip the case-insensitivity of keys separately, we may want to extend the code to distinguish that case from a blanket "--ignore-case".


The implementation of "git branch --sort"(man) wrt the detached HEAD display has always been hacky, which has been cleaned up with Git 2.31 (Q1 2021).

See commit 4045f65, commit 2708ce6, commit 7c269a7, commit d094748, commit 75c50e5 (07 Jan 2021), and commit 08bf6a8, commit ffdd02a (06 Jan 2021) by Ævar Arnfjörð Bjarmason (avar).
(Merged by Junio C Hamano -- gitster -- in commit 9e409d7, 25 Jan 2021)

branch: show "HEAD detached" first under reverse sort

Signed-off-by: Ævar Arnfjörð Bjarmason

Change the output of the likes of "git branch -l --sort=-objectsize"(man) to show the "(HEAD detached at <hash>)" message at the start of the output.
Before the compare_detached_head() function added in a preceding commit we'd emit this output as an emergent effect.

It doesn't make any sense to consider the objectsize, type or other non-attribute of the "(HEAD detached at <hash>)" message for the purposes of sorting.
Let's always emit it at the top instead.
The only reason it was sorted in the first place is because we're injecting it into the ref-filter machinery so builtin/branch.c doesn't need to do its own "am I detached?" detection.


With Git 2.35 (Q1 2022), things like "git -c branch.sort=bogus branch new HEAD"(man), i.e.
the operation modes of the "git branch"(man) command that do not need the sort key information, no longer errors out by seeing a bogus sort key.

See commit 98e7ab6, commit 1a89796 (20 Oct 2021) by Junio C Hamano (gitster).
(Merged by Junio C Hamano -- gitster -- in commit 5126145, 29 Nov 2021)

for-each-ref: delay parsing of --sort=<atom> options

The for-each-ref family of commands invoke parsers immediately when it sees each --sort=<atom> option, and die before even seeing the other options on the command line when the <atom> is unrecognised.

Instead, accumulate them in a string list, and have them parsed into a ref_sorting structure after the command line parsing is done.
As a consequence, "git branch --sort=bogus -h"(man) used to fail to give the brief help, which arguably may have been a feature, now does so, which is more consistent with how other options work.

1
  • To list remotes with this option, add -r Commented Oct 26, 2018 at 15:39
44

I also needed colors, tags and remote references without any duplicates:

for ref in $(git for-each-ref --sort=-committerdate --format="%(refname)" refs/heads/ refs/remotes ); do git log -n1 $ref --pretty=format:"%Cgreen%cr%Creset %C(yellow)%d%Creset %C(bold blue)<%an>%Creset%n" | cat ; done | awk '! a[$0]++'

Because quoting can be hard, here is the alias for Bash:

alias glist='for ref in $(git for-each-ref --sort=-committerdate --format="%(refname)" refs/heads/ refs/remotes ); do git log -n1 $ref --pretty=format:"%Cgreen%cr%Creset %C(yellow)%d%Creset %C(bold blue)<%an>%Creset%n" | cat ; done | awk '"'! a["'$0'"]++'"
8
  • $ <your command here> awk: syntax error near line 1 awk: bailing out near line 1 Commented Nov 8, 2013 at 9:12
  • @GotNoSugarBaby You are using single quotes like the example right? which shell are you using? Bash gives that character a special meaning otherwise.
    – estani
    Commented Nov 8, 2013 at 10:19
  • hey, I ran this on /bin/bash (GNU bash, version 4.0.28(1)-release (i386-pc-solaris2.11)) with a straight copy and paste of your example — but since then I've run it on /bin/bash (GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin12)) and it works, so I'll remove the down-vote. Thanks a lot estani. Commented Nov 8, 2013 at 14:12
  • @GotNoSugarBaby I use 4.2.25(1)-release (x86_64-pc-linux-gnu) nad tried on 3.x and worked. I'm not sure what issue it was... but it might have bennn a git version related issue, instead of a bash one. In any case, I'm glad it works for you!
    – estani
    Commented Nov 25, 2013 at 9:47
  • 1
    @MichaelDiscenza just pipe everything to head. that would be to add | head -n20 at the end. If you are using the alias, be sure this goes within the quotes.
    – estani
    Commented Feb 28, 2015 at 13:42
38

As of Git 2.19 you can simply:

git branch --sort=-committerdate

You can also:

git config branch.sort -committerdate

So whenever you list branches in the current repository, it will be listed sorted by committerdate.

If whenever you list branches, you want them sorted by comitterdate:

git config --global branch.sort -committerdate

Disclaimer: I'm the author of this feature in Git, and I implemented it when I saw this question.

8
  • 3
    Most up-to-date answer, far easier than using complex scripts or aliases 👌
    – mtefi
    Commented Sep 17, 2018 at 11:38
  • Use with caution! Beware everyone, this is not a command to list the branches. It's a command to change the configuration of Git and will have permanent global repercussions.
    – Jazimov
    Commented Nov 13, 2018 at 17:51
  • 1
    @Jazimov you are right, I edited the answer so it's clearer
    – smaftoul
    Commented Dec 1, 2018 at 21:35
  • thank you for adding this neat feature, @smaftoul, i use it a lot. ; ) Commented Jun 18, 2022 at 17:40
  • 1
    @theartofrain you're welcome, I also use it a lot ;)
    – smaftoul
    Commented Jun 20, 2022 at 7:35
33

Another variation:

git branch -r --sort=-committerdate --format='%(HEAD)%(color:yellow)%(refname:short)|%(color:bold green)%(committerdate:relative)|%(color:blue)%(subject)|%(color:magenta)%(authorname)%(color:reset)' --color=always | column -ts'|'

It is worth noting that even though it's looking at changes in remote branches, it's worth synchronising with origin before running the command (you can use Git fetch), as I found it can return out of date information if your local Git folder hasn't been updated in a while.

Also, this is a version that works in Windows cmd and PowerShell:

git branch -r --sort=-committerdate --format="%(HEAD)%(color:yellow)%(refname:short)|%(color:bold green)%(committerdate:relative)|%(color:blue)%(subject)|%(color:magenta)%(authorname)%(color:reset)" --color=always
3
  • 1
    Thanks for the answer for Windows Cmd/Powershell. I can confirm it also works in Cmder.
    – Francis
    Commented Mar 30, 2021 at 4:12
  • Drop the -r ifi you want your local branches, and not just what's been pushed/synced to your remote(s).
    – Zach Young
    Commented Sep 16, 2022 at 19:48
  • 1
    Thank you, you checked remote branches too, that is I want!
    – Ninja
    Commented Aug 4, 2023 at 3:17
30

Getting just the top five branch names sorted based on committer date:

git branch --sort=-committerdate | head -5

26

The other answers don't seem to allow passing -vv to get verbose output.

So here's a one-liner that sorts git branch -vv by commit date, preserving color etc:

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

If you additionally want to print the commit date, you can use this version instead:

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-

Sample output:

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

It's probably more readable split into multiple lines:

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-

This should also work with other arguments to git branch, e.g. -vvr to list remote-tracking branches, or -vva to list both remote-tracking and local branches.

6
  • -vv can be useful indeed, thanks. However, this solution still spawns new processes for each branch, which the OP wanted to avoid.
    – musiphil
    Commented Jan 19, 2014 at 7:28
  • Actually git branch doesn't specifically define the meaning of -vv, but only of -v, so -vv should have the same as -v.
    – musiphil
    Commented Jan 19, 2014 at 7:31
  • 3
    This is the best. And adding -avv makes it take into account remote branches as well. Thanks for this!
    – Gopherkhan
    Commented Apr 21, 2015 at 23:32
  • @musiphil My git branch manpage, section -v, -vv, --verbose contains the following: If given twice, print the name of the upstream branch, as well
    – Perleone
    Commented Sep 28, 2015 at 12:07
  • @Perleone: I don't know how I got that information, but you are right, and I stand corrected. Thanks!
    – musiphil
    Commented Sep 28, 2015 at 17:09
26

I find the following command helpful for my purposes.

git branch --sort=-committerdate | head -n 10

This will list the latest 10 branches. It is short and can be used without an alias as well.

1
  • git config --global alias.br "! git branch --sort=-committerdate | head -n 10"
    – mfink
    Commented Jan 31, 2022 at 19:59
21

I like using a relative date and shortening the branch name like this:

git for-each-ref --sort='-authordate:iso8601' --format=' %(authordate:relative)%09%(refname:short)' refs/heads

Which gives you output:

21 minutes ago  nathan/a_recent_branch
6 hours ago     master
27 hours ago    nathan/some_other_branch
29 hours ago    branch_c
6 days ago      branch_d

I recommend making a Bash file for adding all your favorite aliases and then sharing the script out to your team. Here's an example to add just this one:

#!/bin/sh

git config --global alias.branches "!echo ' ------------------------------------------------------------' && git for-each-ref --sort='-authordate:iso8601' --format=' %(authordate:relative)%09%(refname:short)' refs/heads && echo ' ------------------------------------------------------------'"

Then you can just do this to get a nicely formatted and sorted local branch list:

git branches

Update: Do this if you want coloring:

#!/bin/sh
#
(echo ' ------------------------------------------------------------‌​' && git for-each-ref --sort='-authordate:iso8601' --format=' %(authordate:relative)%09%(refname:short)' refs/heads && echo ' ------------------------------------------------------------‌​') | grep --color -E "$(git rev-parse --abbrev-ref HEAD)$|$"
4
  • This gives me fatal: unknown field name: '-authordate:iso8601' Commented Sep 30, 2015 at 15:04
  • 2
    Fancy colored output is fancy, but this is simple and just what I was looking for. Replace refs/heads with refs/remotes to have a look at remote branches.
    – Lambart
    Commented Apr 30, 2017 at 18:40
  • The command itself is lovely, but the alias throws an error: expansion of alias 'branches' failed; 'echo' is not a git command Commented Apr 13, 2018 at 21:24
  • Works for me. What happens if you just copy paste this into terminal? (echo ' ------------------------------------------------------------‌​' && git for-each-ref --sort='-authordate:iso8601' --format=' %(authordate:relative)%09%(refname:short)' refs/heads && echo ' ------------------------------------------------------------‌​') | grep --color -E "$(git rev-parse --abbrev-ref HEAD)$|$"
    – n8tr
    Commented Apr 15, 2018 at 21:38
13

I came up with the following command (for Git 2.13 and later):

git branch -r --sort=creatordate \
    --format "%(creatordate:relative);%(committername);%(refname:lstrip=-1)" \
    | grep -v ";HEAD$" \
    | column -s ";" -t

If you don’t have column you can replace the last line with

    | sed -e "s/;/\t/g"

The output looks like

6 years ago             Tom Preston-Werner  book
4 years, 4 months ago   Parker Moore        0.12.1-release
4 years ago             Matt Rogers         1.0-branch
3 years, 11 months ago  Matt Rogers         1.2_branch
3 years, 1 month ago    Parker Moore        v1-stable
12 months ago           Ben Balter          pages-as-documents
10 months ago           Jordon Bedwell      make-jekyll-parallel
6 months ago            Pat Hawks           to_integer
5 months ago            Parker Moore        3.4-stable-backport-5920
4 months ago            Parker Moore        yajl-ruby-2-4-patch
4 weeks ago             Parker Moore        3.4-stable
3 weeks ago             Parker Moore        rouge-1-and-2
19 hours ago            jekyllbot           master

I wrote a blog post about how the various pieces work.

2
  • Nice. +1. It does use the git branch --sort I mentioned in stackoverflow.com/a/33163401/6309.
    – VonC
    Commented Aug 1, 2017 at 4:34
  • @DanNissenbaum Make sure you’re using Git 2.13 (released in May 2017) or later.
    – bdesham
    Commented Nov 19, 2017 at 2:54
12

Adds some color (since pretty-format isn't available)

[alias]
    branchdate = for-each-ref --sort=-committerdate refs/heads/ --format="%(authordate:short)%09%(objectname:short)%09%1B[0;33m%(refname:short)%1B[m%09"
0
11

Git v2.19 introduces branch.sort configuration option (see branch.sort).

So git branch will sort by committer date (descending) by default with

# gitconfig
[branch]
    sort = -committerdate     # Descending

Script:

git config --global branch.sort -committerdate

So,

git branch

Output:

* dev
  master
  _

And

git branch -v

Output:

* dev    0afecf5 Merge branch 'oc' into dev
  master 652428a Merge branch 'dev'
  _      7159cf9 Merge branch 'bashrc' into dev
0
9

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:

$ 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

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/

1
  • 5
    That name might not help as I am pretty sure there are a few pieces of software out there with the same name.
    – thoroc
    Commented Jan 29, 2016 at 9:14
9

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:

$ git reflog | egrep -io "moving from ([^[:space:]]+)" | awk '{ print $3 }' | head -n5
master
stable
master
some-cool-feature
feature/improve-everything

See also: How can I get a list of Git branches that I've recently checked out?

7

Here's a little script that I use to switch between recent branches:

#!/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

Using those two aliases:

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

Just call that in a Git repository, 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:

Enter image description here

1
5

Normally we consider the remote branches recently. So try this

git fetch
git for-each-ref --sort=-committerdate refs/remotes/origin
5

Here is another script that does what all the other scripts do. In fact, it provides a function for your shell.

Its contribution is that it pulls some colours from your Git configuration (or uses defaults).

# Git Branch by Date
# Usage: gbd [ -r ]
gbd() {
    local reset_color=`tput sgr0`
    local subject_color=`tput setaf 4 ; tput bold`
    local author_color=`tput setaf 6`

    local target=refs/heads
    local branch_color=`git config --get-color color.branch.local white`

    if [ "$1" = -r ]
    then
        target=refs/remotes/origin
        branch_color=`git config --get-color color.branch.remote red`
    fi

    git for-each-ref --sort=committerdate $target --format="${branch_color}%(refname:short)${reset_color} ${subject_color}%(subject)${reset_color} ${author_color}- %(authorname) (%(committerdate:relative))${reset_color}"
}
4

This is based on saeedgnu's version, but with the current branch shown with a star and in color, and only showing anything that is not described as "months" or "years" ago:

current_branch="$(git symbolic-ref --short -q HEAD)"
git for-each-ref --sort=committerdate refs/heads \
  --format='%(refname:short)|%(committerdate:relative)' \
  | grep -v '\(year\|month\)s\? ago' \
  | while IFS='|' read branch date
    do
      start='  '
      end=''
      if [[ $branch = $current_branch ]]; then
        start='* \e[32m'
        end='\e[0m'
      fi
      printf "$start%-30s %s$end\\n" "$branch" "$date"
    done
4
git for-each-ref --sort=-committerdate refs/heads/

# Or using Git branch (since version 2.7.0)
git branch --sort=-committerdate  # Descending
git branch --sort=committerdate  # Ascending
1
  • An explanation would be in order. E.g., what is the idea/gist? How does it contrast with the previous 30 answers? From the Help Center: "...always explain why the solution you're presenting is appropriate and how it works". Please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today). Commented Jun 14, 2022 at 9:30
3

My best result as a script:

git for-each-ref --sort=-committerdate refs/heads/ --format='%(refname:short)|%(committerdate:iso)|%(authorname)' |
    sed 's/refs\/heads\///g' |
    grep -v BACKUP  | 
    while IFS='|' read branch date author
    do 
        printf '%-15s %-30s %s\n' "$branch" "$date" "$author"
    done
1
  • 2
    What kind of script? Bash? Commented Oct 2, 2019 at 21:39
3

The accepted command-line answer rocks, but if you want something prettier, like a GUI, and your origin === "github".

You can click "Branches" in the repository. Or hit the URL directly: https://github.com/ORGANIZATION_NAME/REPO_NAME/branches

3

The simplest one to print along with the last commit date:

git branch --all  --format='%(committerdate:short) %(refname:short)'|sort
2

Here's the variation I was looking for:

git for-each-ref --sort=-committerdate --format='%(committerdate)%09%(refname:short)' refs/heads/ | tail -r

That tail -r reverses the list so the most-recent commiterdate is last.

2
  • 5
    You can also change --sort=-committerdate to --sort=committerdate to accomplish this.
    – rephorm
    Commented Aug 13, 2014 at 15:52
  • Which tail has -r? Commented Oct 23, 2014 at 8:11
2

I know there are a lot of answers already, but here are my two cents for a simple alias (I like to have my most recent branch at the bottom):

[alias]
        br = !git branch --sort=committerdate --color=always | tail -n15
[color "branch"]
        current = yellow
        local = cyan
        remote = red

This will give you a nice overview of your latest 15 branches, in color, with your current branch highlighted (and it has an asterisk).

2

I didn't find exactly what I was looking so here is my version based on https://stackoverflow.com/a/66134817/1958520, which:

  • works under Windows (but you have to have awk installed - mine came with MinGW, can be installed with Git, Cygwin, ....)
  • is not based on complex aliases to be usable as one-liner in command line / script
  • is colorized
  • uses recent git branch --sort=committerdate
  • output is aligned to columns

So here it is:

git branch -a --sort=committerdate --format='%(HEAD)%(color:yellow)%(refname:short)@SEP@%(color:bold green)%(committerdate:relative)@SEP@%(color:magenta)%(authorname)%(color:reset)@SEP@%(color:blue)%(subject)' --color=always | awk -F "@SEP@" '{printf("%-100s %-30s %-20s %s \n", $1, $2, $3, $4)}'

When inside cmd script it has to be modified to handle %:

git branch -a --sort=committerdate --format="%%(HEAD)%%(color:yellow)%%(refname:short)@SEP@%%(color:bold green)%%(committerdate:relative)@SEP@%%(color:magenta)%%(authorname)%%(color:reset)@SEP@%%(color:blue)%%(subject)" --color=always | awk -F "@SEP@" '{printf("%%-100s %%-30s %%-20s %%s \n", $1, $2, $3, $4)}'

Screenshot for openapi-generator github repo

Notes, howto:

  • use --sort=-committerdate to reverse the sort order
  • numbers and hyphens in awk printf spec "%-100s %-30s %-20s %s \n" are columns length and aligning specifications so this is a place for customization
  • @SEP@ is used as a unique separator string, which then awk uses to match data columns via -F "@SEP". Overkill but avoids accidental confusion when using some simple separator

Not the answer you're looking for? Browse other questions tagged or ask your own question.