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

git stash

Stash your current changes with a message to help you remember

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

git stash list

See stash summary

git stash show

See stash full diff

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

git stash pop

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

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:

git stash apply

How to remove a stash without applying it

Drop the second stash on the list

git stash drop stash@{1}

Remove all your stashes

git stash clear

How to stash partial work

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

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 *