Jump to: Menu

Print related stored stashes when checkouting a branch


I use git stashes parsimoniously and generally prefer to do a WIP commit when I must leave a branch. Yet, sometimes, when I struggle to find a good implementation, I cannot resolve myself to commit the code -even as a WIP- and prefer to stash it away.
So git stashes are places where I put my half-baked attempts for an hypothetic later use.

As soon as I checkout a branch, I call git log to get back into the swing, if the top commit is labelled as WIP, I soft reset it and am right back to work. Checking available stashes on checkout is not as natural for me and I forget to do it half the time thus losing the code stashed or wasting my time rewriting it :sob:

I figured today that I could use the fact that default stashes messages are of the form “WIP on <branchname>:” to grep them just after performing a checkout. That gave birth to my .gitconfig co alias :

1co = "!f() { git checkout \"$@\" && git stash list | grep \"$1:\"; }; f"

… and gives following display :

1$ git co master 
2Switched to branch 'master' 
3Your branch is up-to-date with 'origin/master'. 
4stash@{0}: WIP on master: c35edf60 add Curriculum vitae redirect 
5stash@{2}: WIP on master: 0977c37 Move <head></head> outside head.html 

Tadaam! No more excuses to let stashes rot.