Git Stash for Quick Patches

Git Stash for Quick Patches

March 26, 2009   ·   By Sheldon Conaty

For source control management I'm currently using git, actually my repository is hosted on GitHub (a fantastic service and highly recommended). Git can take a little getting used to but it has several great features that subversion just doesn’t have. My current favorite is the ability to ’stash’ work-in-progress.

We’ve all been there; you’re happily working on the code, halfway through a big change, when someone asks you to quickly fix a bug. Traditionally your only option was to checkout the repository head into another directory so you could make your change there – without being forced to commit or revert the current changes you were working on. But no longer, ‘git stash’ to the rescue!

Now, you can quickly park the work you are doing using...

git stash

This will record the current state of the working directory, saving your modifications and reverting the working directory so that it matches the head of the repository. Now you can make the small tweak to fix the bug without losing your changes. The ‘git commit’ and ‘git push’ the fix as normal. When you are happy the patch is complete type…

git stash apply

Now your saved modifications are restored and you can continue adding that great feature you were working on.

Of course if losing your changes wasn’t a concern you could have used…

git reset --hard HEAD

And all your local changes would have been lost, reverting your working copy to the latest repository head.

Useful Git Resources

 

Post Comments

blog comments powered by Disqus

© Copyright 2009-2012 Peer Assembly | All Rights Reserved.