12

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?

4 Answers 4

23

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

1
  • How would you filter the results? it is possible with git log, e.g --author="Saurav" or --after="2022-06-01 15:00"
    – Mikel
    Commented Jun 3, 2022 at 14:51
1

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
1

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))'
0

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.