Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
List remote Git branches and the last commit date for each branch. Sort by most recent commit date.
# Credit http://stackoverflow.com/a/2514279
for branch in `git branch -r | grep -v HEAD`;do echo -e `git show --format="%ci %cr" $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-02-12 03:20:24 -0800 9 hours ago origin/master
2012-02-10 10:34:35 -0800 2 days ago origin/3-2-stable
2012-01-31 09:56:12 -0800 12 days ago origin/3-1-stable
2012-01-24 11:18:06 -0800 3 weeks ago origin/3-0-stable
2011-12-31 05:09:14 -0800 6 weeks ago origin/2-3-stable
2011-11-25 09:49:54 +0000 3 months ago origin/serializers
2011-06-16 12:08:26 -0700 8 months ago origin/compressor
2011-05-24 16:03:41 -0700 9 months ago origin/sass-cleanup
2011-01-17 14:14:24 +1300 1 year, 1 month ago origin/2-1-stable
2011-01-17 14:13:56 +1300 1 year, 1 month ago origin/2-2-stable
2010-08-17 17:11:17 -0700 1 year, 6 months ago origin/deps_refactor
2010-05-16 22:23:44 +0200 1 year, 9 months ago origin/encoding
2009-09-10 17:41:18 -0700 2 years, 5 months ago origin/2-0-stable
2008-02-19 02:09:55 +0000 4 years ago origin/1-2-stable
@AABoyles

This comment has been minimized.

Show comment
Hide comment
@AABoyles

AABoyles May 17, 2013

Nice! Thanks a lot!

Nice! Thanks a lot!

@bryancdickson

This comment has been minimized.

Show comment
Hide comment
@bryancdickson

bryancdickson Jul 31, 2013

well done. Saved me lots of work.

well done. Saved me lots of work.

@softwaregravy

This comment has been minimized.

Show comment
Hide comment

thanks

@awienber

This comment has been minimized.

Show comment
Hide comment
@awienber

awienber Mar 20, 2014

thanks! and first calling "git remote prune origin" helps to only show the branches still existing in the remote repo.

thanks! and first calling "git remote prune origin" helps to only show the branches still existing in the remote repo.

@FrozenCow

This comment has been minimized.

Show comment
Hide comment
@FrozenCow

FrozenCow Apr 7, 2014

I also wanted local branches to show up, but when changing git branch -r to git branch -a things get messy due to the * that is suffixed to HEAD. After some searching I came up with a single command that has very similar results:

git for-each-ref --format='%(committerdate:iso8601) %(committerdate:relative) %(refname)' --sort -committerdate

Note that this shows heads, remotes and tags. If you only want heads you could use:

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

I also wanted local branches to show up, but when changing git branch -r to git branch -a things get messy due to the * that is suffixed to HEAD. After some searching I came up with a single command that has very similar results:

git for-each-ref --format='%(committerdate:iso8601) %(committerdate:relative) %(refname)' --sort -committerdate

Note that this shows heads, remotes and tags. If you only want heads you could use:

git for-each-ref --format='%(committerdate:iso8601) %(committerdate:relative) %(refname)' --sort -committerdate refs/heads/
@Diom

This comment has been minimized.

Show comment
Hide comment
@Diom

Diom Sep 16, 2014

@FrozenCow If you want to avoid the asterix problem then you can do

for branch in `git branch -l | grep -v '*'`;do echo -e `git show --format="%ci %cr" $branch | head -n 1` \\t$branch; done | sort -r

But it will leave out your current branch.

Diom commented Sep 16, 2014

@FrozenCow If you want to avoid the asterix problem then you can do

for branch in `git branch -l | grep -v '*'`;do echo -e `git show --format="%ci %cr" $branch | head -n 1` \\t$branch; done | sort -r

But it will leave out your current branch.

@allantokuda

This comment has been minimized.

Show comment
Hide comment
@allantokuda

allantokuda Oct 18, 2014

Adding to @FrozenCow, you can make the results tabular with the column utility:

git for-each-ref --format='%(committerdate:short),%(authorname),%(refname:short)' --sort=committerdate refs/heads/ | column -t -s ','

Adding to @FrozenCow, you can make the results tabular with the column utility:

git for-each-ref --format='%(committerdate:short),%(authorname),%(refname:short)' --sort=committerdate refs/heads/ | column -t -s ','
@triage

This comment has been minimized.

Show comment
Hide comment
@triage

triage Jan 31, 2015

thank you!

triage commented Jan 31, 2015

thank you!

@eksperimental

This comment has been minimized.

Show comment
Hide comment
@eksperimental

eksperimental Feb 11, 2015

I think this one has been updated, using colors with pretty format.

for k in `git branch -r | perl -pe 's/^..(.*?)( ->.*)?$/\1/'`; do echo -e `git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k -- | head -n 1`\\t$k; done | sort -r

I think this one has been updated, using colors with pretty format.

for k in `git branch -r | perl -pe 's/^..(.*?)( ->.*)?$/\1/'`; do echo -e `git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k -- | head -n 1`\\t$k; done | sort -r
@liuyu121

This comment has been minimized.

Show comment
Hide comment
@liuyu121

liuyu121 Aug 28, 2015

thx u so much and, thanks @eksperimental

thx u so much and, thanks @eksperimental

@stuartgmilton

This comment has been minimized.

Show comment
Hide comment
@stuartgmilton

stuartgmilton Jun 21, 2016

Is it possible to include the creator of the branch?

Is it possible to include the creator of the branch?

@jampow

This comment has been minimized.

Show comment
Hide comment
@jampow

jampow Jul 20, 2016

based on this posts and this stackoverflow topic. I made this command

git for-each-ref --sort=committerdate refs/heads/ --format='%(HEAD) %(color:red)%(objectname:short)%(color:reset);%(color:yellow)%(refname:short)%(color:reset);(%(color:green)%(committerdate:relative)%(color:reset));%(authorname);%(contents:subject)' | column -t -s ';'

jampow commented Jul 20, 2016

based on this posts and this stackoverflow topic. I made this command

git for-each-ref --sort=committerdate refs/heads/ --format='%(HEAD) %(color:red)%(objectname:short)%(color:reset);%(color:yellow)%(refname:short)%(color:reset);(%(color:green)%(committerdate:relative)%(color:reset));%(authorname);%(contents:subject)' | column -t -s ';'

@michaelwills

This comment has been minimized.

Show comment
Hide comment
@michaelwills

michaelwills Aug 11, 2016

@jampow ๐Ÿ‘

@jasonrudolph thanks for the original inspiration!

@jampow ๐Ÿ‘

@jasonrudolph thanks for the original inspiration!

@arulthayalan

This comment has been minimized.

Show comment
Hide comment
@arulthayalan

arulthayalan Nov 9, 2016

Thank you!!!

Thank you!!!

@windse7en

This comment has been minimized.

Show comment
Hide comment
@windse7en

windse7en Mar 15, 2017

Thanks. Nice work.

Thanks. Nice work.

@auerbachb

This comment has been minimized.

Show comment
Hide comment
@auerbachb

auerbachb May 15, 2017

anyone know how to truncate within this?

git for-each-ref --sort=committerdate refs/heads/ --format='%(color:yellow)%(refname:short)%(color:reset);(%(color:green)%(committerdate:relative)%(color:reset));%<(70,trunc)(%(contents:subject)%s)' | column -t -s';'```
prints the `%<(70,trunc)` rather than applying it.

anyone know how to truncate within this?

git for-each-ref --sort=committerdate refs/heads/ --format='%(color:yellow)%(refname:short)%(color:reset);(%(color:green)%(committerdate:relative)%(color:reset));%<(70,trunc)(%(contents:subject)%s)' | column -t -s';'```
prints the `%<(70,trunc)` rather than applying it.
@juanmf

This comment has been minimized.

Show comment
Hide comment
@juanmf

juanmf Oct 4, 2017

This will sort most recently updated 1st.

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))' | grep <remote|author>

juanmf commented Oct 4, 2017

This will sort most recently updated 1st.

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))' | grep <remote|author>
@igbanam

This comment has been minimized.

Show comment
Hide comment
@igbanam

igbanam Jan 16, 2018

Awesome ๐Ÿ‘

igbanam commented Jan 16, 2018

Awesome ๐Ÿ‘

@igbanam

This comment has been minimized.

Show comment
Hide comment
@igbanam

igbanam Jan 16, 2018

@juanmf thanks for showing this in a readable format. It's easier to add this as an alias

igbanam commented Jan 16, 2018

@juanmf thanks for showing this in a readable format. It's easier to add this as an alias

@binarybasher

This comment has been minimized.

Show comment
Hide comment
@binarybasher

binarybasher Mar 5, 2018

Awesome ๐Ÿ‘ , thanks @jasonrudolph

Awesome ๐Ÿ‘ , thanks @jasonrudolph

@sarelvdwalt

This comment has been minimized.

Show comment
Hide comment
@sarelvdwalt

sarelvdwalt Mar 11, 2018

Perhaps use this to remove "origin/" from the mix... makes it easier to quickly copy & paste what remote branches to delete:

for branch in git branch -r --merged | grep -v HEAD; do echo -e git show --format="%ci %cr %an" $branch | head -n 1 \t$branch; done | sed -e 's/[[:blank:]]origin///g' | sort -r

Perhaps use this to remove "origin/" from the mix... makes it easier to quickly copy & paste what remote branches to delete:

for branch in git branch -r --merged | grep -v HEAD; do echo -e git show --format="%ci %cr %an" $branch | head -n 1 \t$branch; done | sed -e 's/[[:blank:]]origin///g' | sort -r

@jknipp

This comment has been minimized.

Show comment
Hide comment
@jknipp

jknipp May 7, 2018

๐Ÿ‘

jknipp commented May 7, 2018

๐Ÿ‘

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