Git Goodies That You Might Not Know About: git stash

Git Goodies That You Might Not Know About: git stash

We sometimes find ourselves with code that does not belong in a commit. This might be because we are on a wrong branch, just trying new things or need to quickly do something else and are not prepared to commit the code yet. Bottom line is we sit with code that we want to persist somehow but that we do not want in our source control history. Enter git stash.

Git stash will save your local modifications away and revert your working directory to match the HEAD commit. You can then at a later stage re-apply these modifications wherever you want.

To stash your changes you first need to add them so git can track them and then you simply run git stash to stash them.

$ git add $ git stash

Your changes have now been pigeon holed and the branch reverted.

There are two ways to re-apply your stashed code.

$ git stash pop

Pop treats all your stashed changes as a first-in last-out queue. Thus running the pop command will apply your last stash and remove it from your stashed items.

$ git stash apply {stash-id}

Apply will apply a specific stash without removing it from your stash list.

You can view all stashed items using

$ git stash list

You can remove all stashed items or a specific stash using clear and drop respectively.

$ git stash clear
$ git stash drop {stash-id}

Simple, effective and awesome. GIT ALL THE THINGS.

Read more here

Also see git bisect

This post was first published on