/ Git

Useful Git Commands: Stash

Tom knows best!

How wonderful would it be to do your job without interruptions? But unfortunately we live in a complex world and intermissions are part of the game.

A particular annoying disturbance is when you are in the middle of a big refactoring or adding a complex function to a software and someone comes to you asking to immediately fix a bug in the same code.

What you need is a function in your VCS to park your modifications, checkout the right version to modify, fix the bug and then return to your code, without copying files around the disk or make meaningless commits. What you need is git stash.

Simple and Powerful

To save your temporary work, you only need to type git stash. Use the flag --include-untracked to save also new files. When you need to go back to your previous situation, use git stash pop. You can stash how many commits you want and replay them in any order and potentially on top of different commits.

Stashing can be useful every time you need to save your partial work or obtain a clean workspace without losing your modifications, for example when pulling from a remote repository on a non-clean working tree. But it can also be used when you realize that you are working on the wrong branch: you have just to stash, checkout the right branch and then pop. Fast and easy.

In the man page you can find other options, such as the creation of a new branch starting from the commit at which the stash was originally created.

Picture by dealingwith taken from Flickr licensed under a Creative Commons Attribution-NonCommercial 2.0 Generic license.