"git stash" is my new best friend

Apr 3, 2013

This is not gonna be any news for a lot of people out there, but I recently discovered the beauty of the git command stash. And even though I feel a little embarrassed about the fact that I did not discover it earlier, I decided to post a quick note about it.

Before I begin, I'd like to explain that in my main project, I am currently the sole developer. That often made me think of git as more of a deployment tool, then the version control application that it is. And because of that, I didn't feel I necessarily had to learn everything git had to offer.

Now back to git stash. It is the saviour at those moments when you are in the middle of a task and someone comes knocking on your shoulder with something veeery urgent. You know that the thing you are in the middle of will take some time to finish and you don't want to commit something half finished that will end up breaking the app. So what do you do? Why, you stash it of course. By simple writing

$ git stash

you stove away the changes you have done so far and you are left with how the branch looked like before you started. Now you can switch to whatever existing branch you want or create a new one, fix that veeery urgent thing and commit it just like you normally do.

Once you are done, just switch back to the branch you were working on and type

$ git stash apply

and you can continue working from were you left off.

What else?

Well, once you start using git stash regularly, you probably find yourself with a number of stashed changes. You can use the command git stash list to see what you have stored, but here comes the first problem. If you are anything like me, then trying to remember what you were working on a month ago is... well, I don't remember.

So you'll need a way to keep your stash organised. The easiest way to do that is to simply write a comment as you are stashing changes away. This is done by using the command git stash save [message]. This way, when you do a git stash list, then you will see your comment as well as the branch you were on when you stashed it away.

If I only knew earlier.... well, serves me right for not looking for it I guess.