How to Clear Stash in Git
In Git, when we are working on some projects simultaneously, often we are required to switch branches to work on something else, and we don’t want to make a push to this half-done work to the remote repository.
Git has the ultimate solution for this, Git Stash, which creates a temporary path in a directory to save our temporary files and enable switching the branch.
Once we go back to the same branch, we can again apply the stashed things back to the branch and continue working until the feature has been completed or a bug has been fixed.
This tutorial will demonstrate how to clear stashes in Git.
Git Stash
Stashing is the process that takes the messy state of our working directory and stores it in a stack of incomplete changes that we can, later on, reapply. We can create, update, or delete the customized stash temporary changes.
We can also provide the name of the stashing files to remember which files have been pushed to the stashing area.
Let’s suppose we have some local modifications. We can check it by running git status
, which will check our current state:
$ git status
If we want to save our incomplete work without committing them, we will use the git stash
command.
$ git stash
Finally, we are free as our working directory is clean and now we can perform any operation or change the branch.
Clear Stash in Git
Now, if we no longer need that stash that we performed, we remove it by the following command specified with the id of the stash, and it will remove it from the storage area. The command to remove the particular stash is the following:
git stash drop <stash_id>
And if we want to remove all the stashes we did in the past, we will accomplish it by performing the following command:
git stash clear
Reapply Stash in Git
As we know, git stash
is not permanent storage, so when we want to resume our work from where we left, we can reinstate the saved changes simply by using the following command:
git stash pop
If we want to keep those changes in the stash, instead of using git stash pop
, we will use git stash apply
. We can save the changes for future use or if we want to use it again for whatever reasons in Git.
Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.
LinkedIn