Skip to content

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
@binarybasher
Copy link

binarybasher commented Mar 5, 2018

Awesome 👍 , thanks @jasonrudolph

@sarelvdwalt
Copy link

sarelvdwalt commented 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

@jknipp
Copy link

jknipp commented May 7, 2018

👍

Copy link

ghost commented Jul 24, 2018

Thank You bro. @juanmf, finally cracked the my requirement with your code

@tomas
Copy link

tomas commented Aug 14, 2018

Here's my take:

git branch -r | grep -v HEAD | while read b; do git log --color --format="%ci _%C(magenta)%cr %C(bold cyan)$b%Creset %s %C(bold blue)<%an>%Creset" $b | head -n 1; done | sort -r | cut -d_ -f2- | sed 's;origin/;;g' | head -10

No backticks required, and uses git log which is somewhat faster than git show on my PC. Output looks like this:

image

To use it in .gitignore the quotes need to be escaped, like this:

  updated_branches = "!f() { git fetch && git branch -r | grep -v HEAD | while read b; do git log --color --format=\"%ci _%C(magenta)%cr %C(bold cyan)$b%Creset %s %C(bold blue)<%an>%Creset\" $b | head -n 1; done | sort -r | cut -d_ -f2- | sed 's;origin/;;g' | head -10; }; f"

Hope it helps! I'm planning to add something similar to a native command line Git client I'm working on (tomas.github.io/jix).

@Mazorius
Copy link

Mazorius commented Dec 7, 2018

For branches you can use this one:

Use -a for local and remote branches, -r for remote only and -l for local only

git branch -[a,l,r] --format="%(committerdate:iso8601), %(committerdate:relative) - %(refname:short)" | grep -v [H]EAD

@drshiki
Copy link

drshiki commented Mar 13, 2019

woow, thx

@gerroon
Copy link

gerroon commented May 4, 2019

@ Mazorius that does not sort at all.

@roschler
Copy link

roschler commented Jul 26, 2019

Is there a way to always get the date instead of the "X days ago" being used for recent dates? I really don't like that "adaptive" format and just want to see the dates. @Mazorius has the format I want for the dates, but it doesn't sort, so I'm hoping to get the best of both worlds.

@Mazorius
Copy link

Mazorius commented Jul 27, 2019

@gerroon & @roschler: You have to use the sort program to get the sorting by age.

Use -a for local and remote branches, -r for remote only and -l for local only

git branch -[a,l,r] --format="%(committerdate:iso8601), %(committerdate:relative) - %(refname:short)" | grep -v [H]EAD | sort

recursive

git branch -[a,l,r] --format="%(committerdate:iso8601), %(committerdate:relative) - %(refname:short)" | grep -v [H]EAD | sort -r

@tXambe
Copy link

tXambe commented Sep 26, 2019

Hello,
How I can execute this script at external repo, for example:

./git-branches-by-commit-date.sh http://github.com/my_repo

Thanks and a greeting

@fazl
Copy link

fazl commented Dec 5, 2019

@tXambe Sorry I didn't have time to look into changing the script to take that parameter - but a quick workaround could be to simply first checkout your repo to your local machine then run the script (without your argument) from inside the repo. Hope it helps..

@danyill
Copy link

danyill commented Jan 24, 2020

Using awk to truncate the commit message but otherwise re-using from above with colour and columns:

  git branch -r | grep -v HEAD | while read b; do git log --color --format="%ci _%C(magenta) %cr^ %C(bold cyan)$b%Creset^ %s^ %C(bold blue)%an%Creset" $b | head -n 1; done | sort -r | cut -d_ -f2- | sed 's;origin/;;g' | awk -F^ -vOFS=^ 'NR{$3=substr($3,1,60)}1' | head -10 | column -t -s '^'

@mrvincenzo
Copy link

mrvincenzo commented Mar 22, 2020

Great work. Thanks!

@NicolasErices
Copy link

NicolasErices commented Jun 17, 2020

Great!!!. Thanks! its work for me

@vthanki
Copy link

vthanki commented Jul 6, 2020

Thank you!

@lichongchong
Copy link

lichongchong commented Aug 12, 2020

太好了,谢谢你。
That's great. Thank you

@vishalbasra
Copy link

vishalbasra commented Sep 14, 2020

Thank you!

@vladdancer
Copy link

vladdancer commented Jan 20, 2021

Thank you Jason!

@ZenoArrow
Copy link

ZenoArrow commented Feb 10, 2021

Put together a different variation based on a few examples: 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'|'

Also, here's a version that works in Windows cmd and PowerShell (didn't find a good alternative for column command, but otherwise works fine): 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

@Aniket7250
Copy link

Aniket7250 commented Mar 19, 2021

Is there any way to automate that extract last commit date without cloning it on local machine

@simon04
Copy link

simon04 commented Mar 24, 2021

Users of tig might want to use tig refs – This outputs all local/remote branches and tags with their timestamp.

@rameshpasham
Copy link

rameshpasham commented Mar 25, 2021

https://gist.github.com/Aniket7250 Am also looking for without cloning to get this functionality ..

@AngelGuante
Copy link

AngelGuante commented Jun 8, 2021

Thanks!

@waqaraz
Copy link

waqaraz commented Aug 11, 2021

Guys, I understand the commands that how can I list all remote/local branches along with when was it last commited, who made the last commit, last commit id, last commit message etc, sorted by committerdate .... but is there any way that I can list all branches that has their last commits within a date range ... just like we have --since and --until for git log ... for example, I want to list all branches that were being last commited before 6 months ago, or say, all branches that were last commited before "Jan 01 2021"

@vraravam
Copy link

vraravam commented Apr 10, 2022

but is there any way that I can list all branches that has their last commits within a date range ... just like we have --since and --until for git log ... for example, I want to list all branches that were being last commited before 6 months ago, or say, all branches that were last commited before "Jan 01 2021"

I would like the same if possible. But, thanks for the current version as well!

@ZenoArrow
Copy link

ZenoArrow commented Apr 10, 2022

Guys, I understand the commands that how can I list all remote/local branches along with when was it last commited, who made the last commit, last commit id, last commit message etc, sorted by committerdate .... but is there any way that I can list all branches that has their last commits within a date range ... just like we have --since and --until for git log ... for example, I want to list all branches that were being last commited before 6 months ago, or say, all branches that were last commited before "Jan 01 2021"

Yes, it's possible. Here's some information on comparing dates in Bash scripts, you'd want to extract the dates from the git output line-by-line, convert them from text to a comparable date format, and compare against your target date to know whether to display them or not:

https://unix.stackexchange.com/questions/84381/how-to-compare-two-dates-in-a-shell

@ZenoArrow
Copy link

ZenoArrow commented Apr 10, 2022

but is there any way that I can list all branches that has their last commits within a date range ... just like we have --since and --until for git log ... for example, I want to list all branches that were being last commited before 6 months ago, or say, all branches that were last commited before "Jan 01 2021"

I would like the same if possible. But, thanks for the current version as well!

See comment above.

@codisfy
Copy link

codisfy commented Aug 13, 2022

Thanks for this

@nvs2022
Copy link

nvs2022 commented Sep 19, 2022

thx man 👍

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