Skip to content

Instantly share code, notes, and snippets.

@jasonrudolph
Created February 12, 2012 20:40
Show Gist options
  • Save jasonrudolph/1810768 to your computer and use it in GitHub Desktop.
Save jasonrudolph/1810768 to your computer and use it in GitHub Desktop.
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
@rameshpasham
Copy link

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

@AngelGuante
Copy link

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

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

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

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 👍

@rsanheim
Copy link

Another way to do this via gitconfig aliases:

rsanheim/dotfiles#34

@MohanSaiTeki
Copy link

You can use the command below if you want to filter the branches that were updated X - days ago.

git branch -r --format="%(committerdate:iso8601), %(committerdate:relative) - %(refname:short)" | grep -v [H]EAD | awk -v dateA="$(date -d "$(date +%Y-%m-%d) - 3 days" +%Y-%m-%d)" -F '|' 'dateA<=$1" "$2' | awk -F '/' '{ print $NF }'

From the above command replace the 3 days with your X no of days

@jmccay-work
Copy link

Here's my version of this.

# Credit https://gist.github.com/jasonrudolph/1810768
# Credit http://stackoverflow.com/a/2514279
for branch in `git branch -r | grep -v HEAD`;
	do printf "%-40s\\t%20s\\n" $branch "`git show --format="%ci %cr" $branch | head -n 1`"; 
done | sort -k 2

@bjeanes
Copy link

bjeanes commented Aug 8, 2024

Here's a version sorting by committer date, including colour formatting, that is easy to add as a git alias:

❯ git branch -a --sort=-creatordate --format='%(color:red)%(committerdate:iso8601)%(color:reset) %(align:8)(%(ahead-behind:HEAD))%(end) %(color:blue)%(align:40)%(refname:short)%(end)%(color:reset) %(color:white)%(contents:subject) %(color:yellow)(%(committerdate:relative))%(color:reset)'

Example output:

Screenshot 2024-08-08 at 11 41 20 am

Git aliases:

[alias]
        br = "branch --format='%(color:red)%(committerdate:iso8601)%(color:reset) %(align:8)(%(ahead-behind:HEAD))%(end) %(color:blue)%(align:40)%(refname:short)%(end)%(color:reset) %(color:white)%(contents:subject) %(color:yellow)(%(committerdate:relative))%(color:reset)' --sort=-creatordate"
        newestb = "br --sort=-committerdate"
        oldestb = "br --sort=committerdate"

It can then be composed with other flags like -r, -a, --merged=main, etc.

@YPCrumble
Copy link

@bjeanes this looks really nice but I get this error:

fatal: unknown field name: ahead-behind:HEAD

@bjeanes
Copy link

bjeanes commented Aug 13, 2024 via email

@sankalp-khare
Copy link

Great thread, thank you!

@YPCrumble
Copy link

@bjeanes was on 2.39.0, upgrading to the latest version of git solved the issue. Thank you!

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