How to stash changes in GIT – git stash command


The previous tutorial has explained about committing the changes in GIT. This tutorial explains about stashing the changes in GIT.

What is git stash?

git stash temporarily stash or shelves the changes made in working copy so that we can work on something else and later can apply back these changes.

Imagine a situation where you are working on a updating a feature, but suddenly there is a production bug on existing functionality of the feature which needs to be fixed immediately. So, we need to switch to bug fix and leave working on updation work. As the updation work is not completed, we can stash these changes and work on the bug fix. Later we can re-apply our stashed changes back to the code.

The git stash command can stash the uncommitted changes (both staged and unstaged), saves them away for later use, and then reverts them from your working copy. 

How to reapply stashed changes?

To re-apply the stashed changes, use this command

git stash pop

This command removes the changes from your stash and reapplies them to your working copy.

Alternatively, you can reapply the changes to your working copy and keep them in your stash with : 

git stash apply

Stashing untracked or ignored files

If a new file is created and not staged, then git stash is not going to stash that file.

Adding the -u option (or –include-untracked) tells git stash to also stash your untracked files:

git stash -u

Congratulation!! We have learnt about stash in GIT.


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 )

Facebook photo

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

Connecting to %s