Last Updated: November 15, 2018
· jacaetevha

Git the last 10 branches you've worked on


I use an alias in my ~/.gitconfig to quickly see the last 10 branches I've worked on (especially helpful when jumping back and forth between branches).

Update: I somehow missed the --count argument to the for-each-ref command (thanks @hauleth), so instead of this being a shell function, it's a simple alias.

    recent-branches='for-each-ref --sort=-committerdate --count=10 --format='%(refname:short)' refs/heads/'

15 Responses
Add your response


Why that way and not simply:

  recent-branches = for-each-ref --sort=-committerdate --count=10 --format='%(refname:short)' refs/heads/
over 1 year ago ·

@hauleth thanks, updated the post. I didn't see the --count param.

However, if I wanted to be able to pass the count in the future, the function version would come in handy.

over 1 year ago ·

@jacaetevha so what's the problem? Then you simply write:

git recent-branches --count=1

I will override previous flag.

over 1 year ago ·

I just paste it to .gitconfig but the alias expansion failed (not command).

over 1 year ago ·

@hauleth right you are again.

over 1 year ago ·

@drabiter are you able to run the full command?

git for-each-ref --sort=-committerdate --count=10 --format='%(refname:short)' refs/heads/

I would assume you're not able to.

over 1 year ago ·

@jacaetevha I'm able. Time to blame the .gitconfig.

over 1 year ago ·

@drabiter I fixed suggestion like that

recent-branches = for-each-ref --sort=-committerdate --count=10 --format='%(refname:short)' refs/heads/

And it becomes expanded as expected. Try this on your side.

over 1 year ago ·

@zekefast Thanks! it works now :)

over 1 year ago ·

Cool, just a little change, great tip!

# show last 10 branches worked on
recent-branches="!git for-each-ref --sort=-committerdate --count=10 --format='%(refname:short)' refs/heads/"
over 1 year ago ·

@johnantoni -- what does that buy you? Doesn't that launch another git process?

over 1 year ago ·

@johnantoni -- also see this follow-up where I use this alias to implement a branch switching functionality between these branches.

over 1 year ago ·

Thanks for correcting me, you're right http://stackoverflow.com/questions/14489109/how-to-alias-git-checkout-to-git-co

Thanks for the extra tip, that'll make switching between branches easier

over 1 year ago ·

This shows all recent branches that anyone has modified. How do I limit it to just the ones that I've worked on?

over 1 year ago ·

To make this work for only branches that I have committed code on, this is what I settled on:

git config --global alias.recent-branches "! git for-each-ref --sort=-committerdate --count=15 --format='%(authorname),%(color:bold yellow)%(refname:short)%(color:reset) (%(color:bold magenta)%(committerdate:relative)%(color:reset)) %(color:cyan)%(objectname:short)%(color:reset) - %(color:dim white)%(contents:subject)%(color:reset)' refs/heads/ | grep ^Travis | cut -d, -f2-"

This shows:

  • branch name (in yellow)
  • when it was modified (relative to now and in purple)
  • the hash (in cyan)
  • the message from the last commit (in grey)

Change the grep command to use your username if you adopt this approach.

over 1 year ago ·