Coding, Foss

Git Stash

Git’s Stash feature is consistently under used, which is strange because is also one of Git’s features that makes it ridiculously awesome.

Hers’s how it works:

You start working on an issue or feature and have made changes to one or many files in working towards a commit. But before you finish it, you notice that there is an emergency fix you have to make on another issue, or you realize that you have to take up another task before you can commit your current one.

You could in this case just start working on the new task and then carefully stage files for it without including changes from your work for the previous task, but not only is this sloppy, but it is also unlikely to work.

In a case like this, you can just “stash” away the work you have done on your first task and be left with a clean tree to start work on the second.

The Normal Way

All you have to do is go

git stash

You’ll see a return message like

Saved working directory and index state WIP on master: 38fcd98 LAST_COMMIT
HEAD is now at 38fcd98 LAST_COMMIT


To see your saved stash, go

git stash list

You should see a list like

stash@{0}: WIP on master: 38fcd98 LAST_COMMIT
stash@{1}: On master: Stashed Feature X

As you can see, stashes are stacked with the newest ones being on the top.

Once you have tackled the new issue and want to go back to your original work, you want to “apply” your stash by going

git stash apply

This will automatically recover the stash at the top of the pile. But this is not what you always want. You might want to recover a stash from way down the stack. In such a case, just go

git stash apply stash@{1}

The Better Way

When you just got ‘git stash’, the stash message is auto-created making it not very descriptive. So when your stash stack is up to say, 20 commit objects, it might be hard to tell what’s really inside one. You can always go

git stash show stash@{1}

to look inside a stash, but this is not always the quickest way. To tackle this problem, be sure to give your stashes names by going

git stash save "Work on #553"

Now when you go ‘git list’, you’ll see

stash@{0}: On master: Work on #553
stash@{1}: On master: Work on #550
stash@{2}: WIP on master: 38fcd98 LAST_COMMIT
stash@{3}: On master: HELLOsetcap CAP_NET_RAW+eip CAP_NET_ADMIN+eip /usr/bin/dumpcap

As you can see, its a lot easier to tell what might be in the first two stashes here than the last two.

Anyway, also be sure to go

man git stash

as usual, to discover many more cool features of stash.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s