Introduction to using git locally
Wednesday 14th January 2009
Categories: Guides, Code, FLOSS
Stashing
Let's say you're working on the master branch. You're halfway through implementing a new feature, when you find a one line bug. You could fix the bug and finish implementing the new feature, and commit them together. However, this tends to be a bad idea - for instance, what if you revert this feature for some reason? Now you've lost your bug fix too. Generally, we want to keep each commit small and focused, so we want two commits here - one for the bug fix, and one for the new feature.
But how do we write and commit the bug fix without also commiting our half-baked feature implementation? The easiest is by using git-stash. Simply call:
git stash
This will put your changes in both the working directory and the index into the stash, and reset the working directory and index to HEAD. If you want to associate the stash with a different message, then use this command instead:
git stash save <message>
To take a look at what you currently have stashed away, use:
git stash list
To reapply the changes stored in the stash, type:
git stash apply
That will use the latest stash - if you want to use a different stash, then you want:
git stash apply <stash-name>
To remove stashes, you have a few choices. To remove the latest stash:
git stash pop
The following will remove all stashes:
git stash clear
Finally, if you want to remove a particular stash, try:
git stash drop <stash-name>