About Me

My Photo
I'm a colonist who has declared war on machines and intend to conquer them some day. You'll often find me deep in the trenches fighting off bugs and ugly defects in code. When I'm not tappity-tapping at my WMD (also, known as keyboard), you'll find me chatting with friends, reading comics or playing a PC game.

Wednesday, August 22, 2012

Git stashing and why it's awesome!

You're halfway through implementing something on a super-awesome project and haven't committed yet. All of a sudden, your manager runs to your desk and informs you of a major bug in the released version of your project. He says, "Hold the presses! Stop whatever you're doing right now and fix the issue on the release branch..." You look at him curiously, wondering if he just had a beer for lunch.

In an SVN world, you'd have to track what files were being modified and export them out to another location. Rather annoying, to say the least, because it defeats the entire point of using a version control system. You could also create a temporary branch, commit your broken changes and then merge that branch with your working branch afterward. An inelegant solution.

However, you're in a Git world, baby! Enter 'git stash'. Stashing allows you to "file away" some or all of your uncommitted changes. Stashes are pushed into a stack and can be popped back later when you want to resume work.

You sit at your desk while pondering the effects of beer after lunch. Then you look down at your terminal and type:

$ git status -s
 M superawesomefile.cpp

Ah! There is one file that you were working on (there could have been more) but you don't want to commit it just yet. You smile shrewdly to yourself and type:

$ git stash save "mysuperawesome changes"
Saved working directory and index state On master: mysuperawesome changes
HEAD is now at b6c8a3f My last superawesome commit message

But wait, are you sure that your changes were stashed? What if they weren't? Paranoia leads you to type:

$ git stash list
stash@{0}: On master: mysuperawesome changes

$ git stash show stash@{0}
 superawesomefile.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

You list all of your stored stashes with 'git stash list' and then view the files in your stash with 'git stash show'. Sure enough, your superawesomefile is there.

At this point, exuding some major swag, you switch to the release branch and fix the issue with amazing prowess. You then switch back to your working branch and proceed to get back your last stash with:

$ git stash apply
# On branch master
# Changes not staged for commit:
#   (use "git add ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#       modified:   superawesomefile.cpp

You rise to a standing ovation as the crowd goes wild!

EDIT: My friend, Ananth pointed out that in an SVN world, it is possible to achieve stash-like behavior. You can perform a unified diff and redirect output to a file.
svn diff > /path/to/mysuperawesome_changes.diff

Later, when you switch back to the working branch, your saved changes can be applied back as a patch.
patch -p0 -i /path/to/mysuperawesome_changes.diff

Pretty handy that!

1 comment:

j1n M@tt said...

Wow! super cool feature. To be honest, didn't knew there was a feature like this for git.