jhucga
Last Updated: May 17, 2016
·
1.693K
· jacaetevha
D21ebccf20562e370eb0a8cf170a0580

Git the last 10 branches you've worked on

git

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.

[alias]
    recent-branches='for-each-ref --sort=-committerdate --count=10 --format='%(refname:short)' refs/heads/'
Say Thanks
Respond

15 Responses
Add your response

5924
Me

Why that way and not simply:

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

@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 ·
5933
Me

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

git recent-branches --count=1

I will override previous flag.

over 1 year ago ·
5934
Dsc 0105 0

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

over 1 year ago ·
5936
D21ebccf20562e370eb0a8cf170a0580

@hauleth right you are again.

over 1 year ago ·
5937
D21ebccf20562e370eb0a8cf170a0580

@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 ·
5976
Dsc 0105 0

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

over 1 year ago ·
6547
D3a5ecc360da791277fc3807c51a0190

@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 ·
6567
Dsc 0105 0

@zekefast Thanks! it works now :)

over 1 year ago ·
10117
Acb28b517a51f2636824ae77cd5acded

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 ·
10118
D21ebccf20562e370eb0a8cf170a0580

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

over 1 year ago ·
10119
D21ebccf20562e370eb0a8cf170a0580

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

over 1 year ago ·
10122
Acb28b517a51f2636824ae77cd5acded

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 ·
27612

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 ·
27613

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 ·