Stashing the changes in git

We often need to switch branches while working on different features or bug fixes. Most of the times we are in the middle of changing something on Branch A and suddenly we need to switch to different branch to fix something that is very urgent. In such situations, we are not in a position to commit the changes as we have not finished the development yet in current branch. If we switch to the different branch, uncommitted changes are also seen in other branch which we usually do not want. So what to do in these situations? That’s when Git stash comes into picture. We can stash the uncommitted changes which puts the branch in last commit status and working directory becomes clean. Then we can safely switch to other branch, complete the work in that branch then switch to back earlier branch and get the stashed changes back to the working directory. Below image shows that we have modified the file f1.txt in master branch. Below image shows that we have switched to branch b1 and the changes from master branch are also visible there. We do not want that…So let us use stash. To save the changes in stash, you can use below command. git stash save “stash1” After putting the changes on stash, working directory gets clean and you can switch to other branch and after finishing work in other branch you can come back to main branch. Then we can apply the stash so that saved changes would appear in working directory. Applying the stash To apply the most recent stash, you can use below command.
git stash apply
To apply the specific stash, you can use below command.
git stash apply [email protected]{2}
Below image shows the above command in action Clearing the stash items When you apply the stash, that stash is not removed from stash list. You can use below command to remove item from stash.

git stash drop [email protected]1Below command applies the most recent stash and then removes it from stash
git stash pop 
Viewing all items in stash You can view all items in stash using below command.
git stash list
Clearing all entries in stash You can clear all entries from stash using below command.
git stash clear
Here is the output of above example.

Complex problems, Simple Solutions