Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save l15n/3103708 to your computer and use it in GitHub Desktop.

Select an option

Save l15n/3103708 to your computer and use it in GitHub Desktop.
List remote Git branches and the last commit's author and author date for each branch. Sort by most recent commit's author date.
# Credit http://stackoverflow.com/a/2514279
for branch in `git branch -r | grep -v HEAD`;do echo -e `git show --format="%ai %ar by %an" $branch | head -n 1` \\t$branch; done | sort -r
# Example output (resulting from running command on the Rails repo — https://github.com/rails/rails)
2012-06-20 14:21:23 -0700 3 weeks ago by Aaron Patterson origin/master
2012-06-17 01:23:02 -0700 4 weeks ago by José Valim origin/3-2-stable
2012-06-15 20:36:09 +0200 4 weeks ago by Piotr Sarnacki origin/actionview-extraction
2012-06-14 10:55:30 -0700 4 weeks ago by Aaron Patterson origin/3-1-stable
2012-06-12 20:05:46 -0700 4 weeks ago by Aaron Patterson origin/3-0-stable
2012-05-29 10:40:14 +0200 6 weeks ago by Piotr Sarnacki origin/cat-gemfile-test
2012-05-27 14:01:44 +0200 7 weeks ago by Piotr Sarnacki origin/helpers-order-fixes
2012-05-27 01:47:37 +0200 7 weeks ago by Piotr Sarnacki origin/possible-railties-fix
2012-03-29 06:26:32 -0700 4 months ago by Xavier Noria origin/2-3-stable
2012-03-02 15:01:20 +0100 4 months ago by Sergey Nartimov + José Valim origin/opt_routes
2011-11-25 09:49:54 +0000 8 months ago by José Valim origin/serializers
2011-06-16 12:08:26 -0700 1 year, 1 month ago by wycats origin/compressor
2011-05-24 16:03:41 -0700 1 year, 2 months ago by wycats origin/sass-cleanup
2011-01-13 15:42:28 +1300 1 year, 6 months ago by Michael Koziarski origin/2-1-stable
2011-01-13 15:33:38 +1300 1 year, 6 months ago by Michael Koziarski origin/2-2-stable
2010-08-17 17:11:17 -0700 1 year, 11 months ago by Aaron Patterson origin/deps_refactor
2010-05-16 15:20:52 -0300 2 years, 2 months ago by Santiago Pastorino origin/encoding
2009-09-09 22:25:23 -0700 2 years, 10 months ago by Beau Harrington origin/2-0-stable
2008-02-19 02:09:55 +0000 4 years, 5 months ago by Jeremy Kemper origin/1-2-stable
@kursion

kursion commented Aug 18, 2015

Copy link
Copy Markdown

👍

@joequery

Copy link
Copy Markdown

Awesome!

Here's how you use it as a git alias

[alias]
    branchauthors = "! for branch in `git branch -r | grep -v HEAD`;do echo `git show --format=\"%ai %ar by %an\" $branch | head -n 1` \t$branch; done | sort -r"

@joequery

Copy link
Copy Markdown

This works really well with seeing pull requests locally

@Jerakin

Jerakin commented Mar 16, 2016

Copy link
Copy Markdown
for branch in `git branch -r | grep -v HEAD`;do echo -e `git show --pretty=format:"%Cred %cn %>|(40) %Cblue %ar %>|(80) %Creset" $branch | head -n 1` $branch; done | sort -r

For a version that sorts on name, have age and branch. Also formated with colors.
Piotr Sarnacki 7 days ago origin/3-0-stable
Piotr Sarnacki 4 months ago origin/encoding

@tiagobarbosamelo

Copy link
Copy Markdown

👍

@bauertank

Copy link
Copy Markdown

Thanks 👍

@ananthulasrikar

Copy link
Copy Markdown

Awesome thanks so much.

@dmusican

dmusican commented Dec 1, 2017

Copy link
Copy Markdown

Thank you, this is great!

@dmusican

dmusican commented Dec 4, 2017

Copy link
Copy Markdown

Here's one very minor update I can make to the excellent solution by Jerakin:

[alias]
    branchauthors = "! for branch in `git branch -r | grep -v /HEAD`;do echo `git show --format=\"%ai %ar by %an\" $branch | head -n 1` \t$branch; done | sort -r"

By putting the slash in front of HEAD, it doesn't filter out branches that have HEAD within the name (like my_HEAD_branch) or some such.

@ktornow13

Copy link
Copy Markdown

THANK YOU! This was just what i was looking for - wanted to let you know it is still being used!

@AlexanderLill

Copy link
Copy Markdown

Thank you! Same here, appreciate the snippet.

@ichihara-3

Copy link
Copy Markdown

Thank you! This is Great 👍

@Kapcash

Kapcash commented Apr 28, 2020

Copy link
Copy Markdown

We can add the format placeholder %<(N) or %>(N) to fix the width of every data, so that we have clean columns:

for branch in `git branch -r | grep -v HEAD`;do echo -e "`git show --format="%>(25)%an | %<(25)%ar" $branch | head -n 1`" $branch; done | sort -r

@antonklimov

Copy link
Copy Markdown

If you look at what git show does you will see that it produces diffs, which is not necessary for this case. It might make sense to add something like --name-only to speed up things a bit if there are many remote branches.

@lexjacobs

Copy link
Copy Markdown
for branch in `git branch -r | grep -v HEAD`;do echo -e `git show --format="%ai %ar by %an %s" $branch | head -n 1` \\t$branch; done | sort -r

if you also want the commit message (comes form the %s per https://git-scm.com/docs/git-show)

@penggaolai

penggaolai commented Jul 23, 2021

Copy link
Copy Markdown

If I want to export the result to a csv file, output like "...1 year, 1 month ago..." will be in separate columns, is there any way to fix such a case.

@penguin359

Copy link
Copy Markdown

If I want to export the result to a csv file, output like "...1 year, 1 month ago..." will be in separate columns, is there any way to fix such a case.

Change the format string and add commas and/or double-quotes as needed to make it CSV compliant:

git show --format='"%ai","%ar by %an"' $branch

Or whatever you desire.

@gskema

gskema commented Mar 1, 2024

Copy link
Copy Markdown

I need an updated version with Author -> Branch Count list

@chucklu

chucklu commented Mar 5, 2024

Copy link
Copy Markdown

tried to list all local branches
change git branch -r | grep -v HEAD to git branch, and got lots of errors, although it still produce the result

fatal: Needed a single revision
fatal: ambiguous argument '+': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git [...] -- [...]'
fatal: Needed a single revision
fatal: ambiguous argument '+': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:

@penguin359

Copy link
Copy Markdown

tried to list all local branches change git branch -r | grep -v HEAD to git branch, and got lots of errors, although it still produce the result

fatal: Needed a single revision
fatal: ambiguous argument '+': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git [...] -- [...]'
fatal: Needed a single revision
fatal: ambiguous argument '+': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:

Try using git branch --format '%(refname)' instead.

@penguin359

Copy link
Copy Markdown

I need an updated version with Author -> Branch Count list

I think this will get you pretty close:

for branch in `git branch -r | grep -v HEAD`;do echo -e `git show --format="%an" $branch | head -n 1`; done | sort | uniq -c | sort -rn

@flocsy

flocsy commented May 20, 2025

Copy link
Copy Markdown

I need an updated version with Author -> Branch Count list

I think this will get you pretty close:

for branch in `git branch -r | grep -v HEAD`;do echo -e `git show --format="%an" $branch | head -n 1`; done | sort | uniq -c | sort -rn

Very useful!

Here some strange people change their name from time to time or have some funky characters so I improved it a bit:

for branch in `git branch -r | grep -v HEAD`;do echo -e `git show --format="%an" $branch | head -n 1`; done | sed -e 's#[@].*##' | tr '_' ' ' | tr '.' ' ' | tr -d '“”' | tr '[:upper:]' '[:lower:]' | sort | uniq -c | sort -rn```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment