Derek Ekins
Derek Ekins

Reputation: 11391

show git branches with date of last commit

I was working on a branch a couple of weeks ago but I can't remember what the branch was called (there are many). I'd like to be able to do something like:

git branch --print-last-commit

and for it to output something like:

branch 1 - 2017-02-12
branch 2 - 2016-12-30

etc.

Is there any way to do this?

Upvotes: 12

Views: 8339

Answers (4)

techExplorer
techExplorer

Reputation: 908

Command to show local branch in format "branch - Commit message - commit user (commit date). Use -r option for remote branches.

git branch --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:short)%(color:reset))'

Upvotes: 2

Adam Ryman
Adam Ryman

Reputation: 11

I know this post is old, though with the help of other answers, I came out with another solution that does not involve a bash for loop.

$ paste  <(git branch | xargs -I {} git --no-pager show -q --format="%ci %cr" {} | tail -n +1) \
    <(git branch) | sort -h | tail -5
2021-10-12 11:24:21 -0700 2 weeks ago     adamryman/foobar
2021-10-12 15:20:18 -0700 2 weeks ago     adamryman/foobarbaz
2021-10-26 16:46:25 -0700 3 days ago      adamryman/baz
2021-10-27 19:00:14 -0700 2 days ago      adamryman/foobaz
2021-10-28 14:03:48 -0700 21 hours ago    adamryman/barfoo

Upvotes: 1

srr7
srr7

Reputation: 171

You can use below command to get all last commit per branch

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

More info at https://gist.github.com/jasonrudolph/1810768

Upvotes: 1

Saurav Sahu
Saurav Sahu

Reputation: 13994

This will print BranchName - CommitMessage - Date as (YYYY-MM-DD). You can manipulate/edit this command line to suit your need.

git for-each-ref --sort=committerdate refs/heads/ --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:short)%(color:reset))'

Note that it will print for all local branches, not just current branch. You can create an alias for convenience.

[alias]
       branchcommits = !git for-each-ref --sort=committerdate refs/heads/ --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:short)%(color:reset))'

and run git branchcommits in git bash prompt.

enter image description here

Upvotes: 24

Related Questions