Home >>

Git Stash


Git Stash

Although Git is certainly not a new distributed version control system, I am new to using it as of this year. After a good 6+ years of Subversion (SVN) (and CVS before that) I’m still learning the differences that sets Git light years ahead of any other version control system currently available. With new technologies also comes new philosophies. For instance with Git, now it is easier than ever, and encouraged, to have separate short-lived branches locally to do work.

For those not familiar with Git, you will find some similarities to other version control systems. However, Git is unique as it is designed to be social. The ability to branch, and work locally (disconnect from a network if need be) allows for code bases to be more open to the public, and allow the owner of the branch to merge in changes only as they are approved. Instead of working off one repository where some nasty merge issues can occur, Git carries the entire repository along for the ride on each branch and each commit of the branch. The image below shows a graphical representation of how the information is stored.

Of all the cool features, bells, and whistles available with Git, I have recently discovered the stash command which is proving very useful. Because of the nature of branching and keeping branches short lived, often times I find myself needing to switch between branches. This is fairly easy to do, but the nature of Git also carries local unstaged changes from branch to branch. What git stash allows you to do is to “stash” away your local changes on a particular branch while you switch to do work in another branch. When you are ready you can switch back to the branch and “unstash” the changes to pick right back up where you left off.

The git commands to do this are very simple and easy to use. For Example:

To stash files

git stash

To retrieve stashed files

git stash pop

That’s all there really is too it, just make sure stash is run while the branch with the changes you want to stash in is checked out and run stash with the pop option when you have checked back out the same branch to continue the work in. If you are new to Git or have not tried the stash I highly recommend trying it out. If you’re like me you’ll wonder how you ever lived with out it!

Side Note

When looking for graphics for this post, I came across this comic from Mount Saint Awesome.

Published by John Zeren

John Zeren is a software engineering professional with a concentrated background in, and passion for, web application development. As a technical and a people leader in the tech space, he is a champion of agile methodologies, collaboration, and using iterative development to solve complex problems.

Leave a comment

Your email address will not be published. Required fields are marked *