Git stash

 

The git stash command is used to stash away your current work and revert back to a clean working directory which matches the HEAD commit.

Let’s say for example that you are in a middle of working on a feature branch and suddenly a customer issue comes up. At this point you want to commit your code but cannot and you want to save it. This is when git stash comes in handy. You can stash your changes and come back to them later after you fix the customer issue.

Note that git stash is stored locally and is not pushed to your git server.

How to stash
Stash your current changes

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git stash
git stash
git stash

Stash your current changes with a message to help you remember

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git stash save "message"
git stash save "message"
git stash save "message"

You can stash multiple changes in which case they will be in a list.

How to see your stashes

List your stashes

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git stash list
git stash list
git stash list

See stash summary

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git stash show
git stash show
git stash show

See stash full diff

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git stash show -p
git stash show -p
git stash show -p

How to apply back your stash(es)

Apply the first stash to your code and remove that first stash from the list

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git stash pop
git stash pop
git stash pop

Apply the second stash to your code and remove that second stash from the list

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git stash pop stash@{1}
git stash pop stash@{1}
git stash pop stash@{1}

if you want to apply the stash to your code but not remove it from your stash list then use:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git stash apply
git stash apply
git stash apply

How to remove a stash without applying it

Drop the second stash on the list

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git stash drop stash@{1}
git stash drop stash@{1}
git stash drop stash@{1}

Remove all your stashes

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git stash clear
git stash clear
git stash clear

How to stash partial work

This will iterate through your changes and ask you which file to stash

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git stash -p
git stash -p
git stash -p

 

 




Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Leave a Reply

Your email address will not be published. Required fields are marked *