git, show stash list after checkout.

Sometime you start doing something in your codebase, and then you have to go do something else on another branch, so you stash them, later you come back, and you may not remember you started, them, and you forget to check your stash list.

Git has a very general solution for these kind of things: hooks

hooks are actions that you can "hook" (heh!) to events, it's common to use the pre-commit hook to check for various errors before validating it, or to update refs after a fetch. Here, we want to display the list of stashes done on the current branch, after you checkout it.

first, we need to take the current branch.

 branch=$(git branch | grep "^\*"|sed -s s/'* '//)

then, we get the list of stash, and filter it on this branch:

 git stash list | grep "WIP on $branch"

put that in .git/hooks/post-checkout of your repository, and make it executable.

 chmod +x .git/hooks/post-checkout

But now, you have to do that in all your git repositories, but then there is this nice trick that allow you to easily use that hook in all your repositories, with just an unintrusive git init each time.

edit 2014-03-13: adding the exit 0 at the end of the script, because it prevented rebase. the code is now:

1
2
3
4
#!/usr/bin/env sh
branch=$(git branch | grep "^\*"|sed -s s/'* '//)
git stash list | grep "WIP on $branch"
exit 0