17

When I type git branch, I receive a list of branches that appear to be sorted alphabetically instead of being sorted by their creation time.

Is there a way to make the output of git branch sorted by date?

WinWin
  • 7,493
  • 10
  • 44
  • 53
  • 1
    Duplicate of http://stackoverflow.com/questions/5188320/how-can-i-get-a-list-of-git-branches-ordered-by-most-recent-commit and http://stackoverflow.com/questions/2514172/listing-each-branch-and-its-last-revisions-date-in-git/2514279#2514279 – joeln Dec 09 '13 at 21:12

3 Answers3

19

Stujo's answer is my favorite, but I wanted to go one step further and make sort by committer date my default git branch behavior. Here's how:

git config --global branch.sort -committerdate

Remove the - before committerdate to sort the other way.

Now git branch will always be sorted by date!

derpedy-doo
  • 1,097
  • 1
  • 18
  • 22
9

As of git 2.7.0 this will work:

git branch --sort=-committerdate
stujo
  • 2,089
  • 24
  • 29
9

Edit: Since Git version 2.19 (late 2018), Git obeys a branch.sort configuration. Since Git version 2.7.0 (early 2016), git branch itself allows sorting, so that you do not have to use git for-each-ref directly.

Edit

Alas there are apparent problems with the sorting options taken by git-for-each-ref. Since that command is (obviously) explicitely aimed at showing refs and accepts the --sort option, I'm thinking of this as a likely bug[1].

Here is the best options I can further come up with, but the output is quite far estranged from the original format (because they rely on decorating revisions after the fact to refer to branches). Ah well, maybe it is of use for you:


[1] if this were git-rev-list or git-log I would think the problem would be that we're not actually walking a revision tree; we're actively trying to show only tips of trees, without walking them.

Temporary alternative

git log --no-walk --date-order --oneline --decorate \
       $(git rev-list --branches --no-walk)

This would give you a list similar to

4934e92 (HEAD, origin/testing, origin/HEAD, testing) reviewed INSTALL file as per #1331
6215be7 (origin/maint, maint) reviewed INSTALL file as per #1331
1e5e121 (origin/emmanuel, emmanuel) buffers: adjust the size in zfsfuse_stat
e96783e (origin/compress, compress) buffers: adjust the size in zfsfuse_stat
f6e2c6c (origin/unstable, unstable) revert the fatal condition again
dd52720 (origin/master-lucid, master-lucid) lucid
3b32fa7 (tag: 0.7.0, origin/master, master) panic revocation of 0.7.0-0 package necessitates an update
6eaa64f (origin/maint-0.6.9, maint-0.6.9) Replace remount by drop_caches (on rollback)

_As you can see, the result can be a bit overwhelming in the presence of many remote (tracking) branches, which effectively alias the same revisions. However, the result is properly ordered by (descending) date.

The correct (unfortunately broken?) approach...

No, but you should be able to do

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

(use --sort='-*authordate' for author date ordering)

On my test repo, this yields:

compress
emmanuel
maint
maint-0.6.9
master
master-lucid
testing
unstable

Alias

you can create a git alias to do this: append the following lines to .git/config

[alias]
branch2 = git for-each-ref --sort='-*committerdate' --format="%(refname:short)" refs/heads/

From then on, you could just say git branch2

torek
  • 448,244
  • 59
  • 642
  • 775
sehe
  • 374,641
  • 47
  • 450
  • 633
  • This is a great answer! Already +1. It doesn't work for me, however, for some reason. It does sort them differently than `git branch`, though. Any idea what could be the problem? – WinWin Jul 08 '11 at 20:52
  • The .git/config alias tip is remarkable. I tried `--sort='-*authordate'` and `--sort='-*taggerdate'` but they none seems to do the trick of sorting by tagging time. – WinWin Jul 08 '11 at 20:58
  • @WinWin, do you want them sorted by the time they were *created* or by the time they were last committed to? I'm not sure the former would be that easy to get. – ebneter Jul 08 '11 at 21:00
  • 1
    @WinWin, I agree, there seems to be something preventing the sort order to work correctly. I'll update my answer accordingly – sehe Jul 08 '11 at 21:02
  • @ebneter Either will do for now. But none of these work for me. – WinWin Jul 08 '11 at 21:02
  • @ebetner: Sorting by the time the branch was created is, in general, not just hard but impossible. If the branch creation is beyond the length of the reflog (by default 90 days), that's all you'll be able to see. – Cascabel Jul 08 '11 at 23:40
  • @sehe Your "Temporary alternative" works perfectly. Yes, the output is more verbose than `git branch` but I can always use `cut` or `sed` to get desired formatting. Thanks! – WinWin Jul 09 '11 at 17:53
  • @sehe For example, I can get a pretty similar result by piping your output through `| cut -d " " -f 2`. – WinWin Jul 09 '11 at 17:58
  • @sehe And `| cut -d "(" -f 2 | cut -d ")" -f 1` works even better. :) – WinWin Jul 09 '11 at 18:02
  • @sehe One more advantage of your "Temporary alternative": It includes **tags** as well - and in order! (just like `gitk`) – WinWin Jul 10 '11 at 20:13
  • @WinWin: guess what gitk uses (other than `git show-branch`) :) – sehe Jul 10 '11 at 22:31
  • 1
    This answer is no longer relevant as there is a dead simple solution in stunjo's answer – derpedy-doo Nov 07 '19 at 03:46