Useful git commands, volume 1

Time for an edition of Useful Git Commands! This is not going to teach you how to git checkout a branch, though we can definitely cover that if anyone wants it! These are just a couple quick tips that have solved problems I've had in my daily git-using life.

Clean local branches

If you're like me, you have approximately seventeen thousand branches in the main repo you work on, some of which are relevant, but most of which have long since been merged to the remote master and are just sitting there, forlorn.

Let's give those forlorn, decayed branches some peace, shall we? Here's a one-line command to clean out your local git of any branches that have been merged to the current working branch (skips master, dev, or branch-name-you-dont-want-to-delete):

git branch --merged | egrep -v "(^\*|master|dev|branch-name-you-dont-want-to-delete)" | xargs git branch -d

See working branch history

Look, I have a pretty terrible memory, and it's not getting better as I'm getting older either. One place this consistently manifests itself is that I can never, NEVER remember what I named that branch I was just working on yesterday. Luckily, I have this command, which lists the last count branches checked out, aliased to glast so I can get one step closer to never needing to remember anything, ever! (Remember how cool aliasing is? If not, don't worry, just check out this post!)

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

That's all for today. Hope these help your productivity / spring cleaning. Ta-ta for now!