How to Recover Stash in Git
- Stash Changes in Git
- View Stashed Changes in Git
- Recover Stashed Changes in Git
- Delete Stashed Changes in Git
Sometimes while working with Git, we face a situation where we want to pull the latest changes from the remote repository, and it conflicts with the existing modifications or files, then we must have to push these files to the stash.
Git offers us a feature that stores snapshots of the changes we have done without committing them to our repository, and this Git area is called Stash
.
It’s neither associated with our working tree nor the staging area. It is a very efficient feature that temporarily saves our changes and cleans the working tree to start fresh work.
Later as per our need, we can omit or bring back those changes we made and then reapply them in the new branch or some different branch.
This article will discuss git stash
and how to overcome messy Git situations when recovering the stash. This function is used when we have changed a branch that we don’t want to commit, but we want to change over to another branch.
Stash Changes in Git
For saving the changes in the stash for any purpose, we will execute the command:
git stash save "optional message for yourself"
This command mentioned above will save our changes and return them to the working directory. The stashed changes can be available from any branch in that repository.
View Stashed Changes in Git
To view the list of stashed changes, we will execute the command:
git stash list
This command, as mentioned earlier, will show us a list of saved changes in the format stash@{0}
. The stash@{0}
indicates the name of the stash, and the curly braces ({ }
) containing numbers are the index of that stash.
If we have many stashed changes, each set will have a different index according to the saved sequence.
If we have forgotten what changes we have made in the stash, we can view them through the following command, and this below command will show us all the past changes in the stash.
git stash show stash@{0}
Recover Stashed Changes in Git
There are two methods to recover or retrieve our stashed changes from the stashed bucket and apply them to the current branch that we want to work on in the future. Both of the commands are mentioned below one by one:
Git Stash Apply
Through the below-mentioned command, we can apply the changes that will leave the copy in the stash, and we can use it in the future if we want to use it for any reason.
git stash apply STASH-NAME
We can particularly mention the name of the stash if there is more than one stash so that we will get the specified stash only.
git stash apply
If we don’t enter the name, Git will automatically apply the top of the stash, which was pushed last time.
Git Stash Pop
We can also apply changes through the below-mentioned command, but this command will eliminate the files from the stash, and we will not be able to use them in the future.
git stash pop STASH-NAME
Delete Stashed Changes in Git
We will execute the following command whenever we want to remove the stashed changes after applying them. Still, this command will eliminate all the changes from the stash, and we will not be able to use them in the future anymore:
git stash drop STASH-NAME
For cleaning the whole stash, we will run the following command:
git stash clear
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