How to stash changes in GIT – git stash command

HOME

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 stashes or shelves the changes made in the working copy so that we can work on something else and later apply these changes.

Imagine a situation where you are working on updating a feature, but suddenly there is a production bug in the existing functionality of the feature that needs to be fixed immediately. So, we need to switch to a bug fix and leave working on the update work. As the update 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), save them away for later use, and then revert 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 learned about stash in GIT.

Leave a comment