Git stash temporarily stashes(hideout) the changes you’ve made to your working repository so you can work on something else, and then come back and re-apply them later on.
Git Stash command is used to save current working directory changes into stash and cleans current working directory with staged or new changes. Current working directory gets updated to git HEAD version after using Got Stash.
Suppose you have created a file on your repository in your working branch and later on you have added those changes and you realized that this should have been added later, or this should have been applied later, so in this scenario we can use .
Let’s have a look at an example, we will be creating a file in our local system repository and we will be using git stash over there.
We have created a new file with name hello.txt
After performing git status it shows us our untracked file
Now we will add our changes and remember not to commit because it doesn’t work on committed changes,
Now we want to prevent our new changes to be committed we will perform git stash
In the above example we can see that we have applied git stash and it removed our latest changes and then we have pushed to our remote repository, and later when we did ls it didn’t show our latest file that we have added (hello.txt).
Now if we will do git stash pop it will again show our changes and our files and we can add and commit them, in common words git stash pop revert the changes and bring them to the normal situation.
git stash pop
Now as you can see after performing git stash pop it again brings back our file and if we will do ls it will shows us our file that we have created (hello.txt)