As I work on various projects, I tend to end up with a bunch of git branches lying around in my repo. Every once in a while, I run a git branch --merged
on master to see what can be trivially trimmed, but sometimes the list of unmerged branches grows as well.
Today I discovered a nice command to help me sort through this list: git for-each-ref
along with some formatting and bash-fu:
$ git for-each-ref \
--sort=-committerdate refs/heads/ \
--format='%(refname:short):%(committerdate:relative)' \
\
|column -t -s ':'
which results in something like
just-edited 47 minutes ago
master 4 days ago
worked-on-recently 5 days ago
wow-has-it-been-that-long 2 years, 2 months ago
The "one-liner" is a bit long to remember, so this is a perfect candidate for a git alias. (I tend to write mine manually into the .gitconfig
file.)
Since we want to pipe the ouput through column
for nicer output, we need !git
in front of the comand in our .gitconfig
:
[alias]
co = checkout
recent-branches = !git for-each-ref --sort=-committerdate refs/heads/ --format='%(refname:short):%(committerdate:relative)' |column -t -s ':'
My whole .gitconfig
can be found in my dotfiles
Comments !