Git has a very powerful command git stash
that allows you to save your current state of working directory changes and let go back to the previous commit. After making any other modifications, you can revert back to that state of your git branch.
You can get your stashed changes anytime, you can easily create, delete, remove the stash changes from the stored index.
So, today in this tutorial, we will learn about how to make git stash changes stored at a specific state and then use them when we need them.
Git Stash Command
To take a snapshot of your current working branch and store your changes at a specific index, then use the following git stash
command.
When you will run the above command it will save your changes in the current working directory for later use and commit it. It will save branch changes without any specific message indication.
See Also: How to Create a New Git Branch?
But if you want to highlight with a message for that changes for that branch then you can also easily do this. You just have to type the message in double quotes and run the save command with the save parameter.
The above command will save your changes with the message and you can easily indicate when you will revert these changes.
Note: Make sure your changes need to be on tracked files. If you have newly created files and try to stash them, you will get an error.
Stash Changes List
If you want to get the full list of all stash indexes then run the git stash list
command. It will list all the stash lists with an index of each.
When you run the above command it will return the stash list with index number and message as this format stash@{0}: BRANCH-NAME: MESSAGE
. See the following output for reference.
As you can see the above are the git stash list. Then number between {} braces is the index number of state, it used when you want to apply the specific stash changes to the working directory.
Stash Changes Show
Let’s suppose you have a stash changes list and some of them you forget what you did in that stash. So in this case, you want to see the difference between your working git branch and stash changes.
To see the changes and summary of them with your current directory, use the git stash show
command. You just have to run this command with the index number of stash.
You can add the -p flag with this command to see all the differences, what content was added and what was removed.
Stash Changes Retrieve
Now if you want to retrieve the stashed changes into your current working directory then there are two commands that you can use.
git stash pop stash@{index}
The git stash pop
command will merge your changes with your current branch and also remove the files from the stash list.
git stash apply stash@{index}
The git stash apply
command will apply your changes on your current branch but also keep the files on stash. So you can also get to other branches.
Stash Changes Drop
To remove the specific git stash from the stashed list, use the git stash drop
command. It will permanently remove that stash. So be careful while using this command and make sure that stash changes you don’t need anymore.
The above command will remove the stash that has an index number is 2.
If you want to remove all the stash lists then simply use the git stash clear
command. It will clear your stash and you will see no stash is anymore.
Conclusion
So in this tutorial, you learned how you can make git stash changes and store differently from the current working branch.
You also learned how you can print the stashed list, see the differences, what you did in a specific stash and how to revert back into the current branch using two different options.
Any queries or doubts, you are most welcome. Just ask in the comment section, I will respond to you as soon as possible.