Click here to Skip to main content
15,889,732 members
Articles / DevOps / Git
Tip/Trick

Git Goodies That You Might Not Know About: git stash

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
16 Jan 2015CPOL1 min read 8.1K   4   1
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 blog.entelect.co.za

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Unknown
I write about Domain Driven Design, Clean Code and .net

Comments and Discussions

 
GeneralAlso -u|--include-untracked options are available Pin
PhilipOakley19-Jan-15 8:21
professionalPhilipOakley19-Jan-15 8:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.