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.
[alias]
recent-branches='for-each-ref --sort=-committerdate --count=10 --format='%(refname:short)' refs/heads/'
Written by Jason Rogers
Related protips
15 Responses
Why that way and not simply:
[alias]
recent-branches = for-each-ref --sort=-committerdate --count=10 --format='%(refname:short)' refs/heads/
@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.
@jacaetevha so what's the problem? Then you simply write:
git recent-branches --count=1
I will override previous flag.
I just paste it to .gitconfig but the alias expansion failed (not command).
@hauleth right you are again.
@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.
@jacaetevha I'm able. Time to blame the .gitconfig.
@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.
@zekefast Thanks! it works now :)
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/"
@johnantoni -- what does that buy you? Doesn't that launch another git process?
@johnantoni -- also see this follow-up where I use this alias to implement a branch switching functionality between these branches.
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
This shows all recent branches that anyone has modified. How do I limit it to just the ones that I've worked on?
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.