In this article, we will learn How and when to use Git Stash Command.
Total Views: 2761
In this article, we will learn How and when to use Git Stash Command. Suppose you are working on new functionality and have done several changes in multiple files. During that, you got an urgent requirement to fix some bugs in the other branch. But the functionality on which you are working is not completed yet, means not in the condition to commit in the respective branch. This is where the use of stash comes in. The meaning of Stash is to “store something safely in a hidden or secret place”. Using the “Git Stash” command, Git takes the changes from the working area and puts those in the stash. There are multiple git stash commands available like checking the available stash list, saving a stash, deleting a stash, clearing all stash, etc. Use the “git stash -h” help command to check the available commands as well as options.
In this article, we will learn:
1. How to stash changes?
2. How to view the existing stash list?
3. How to apply the existing stash?
4. How to delete a particular stash?
5. How to clear all existing stash?
6. How to create a branch with the stash?
7. How to use the git stash show command?
1. How to stash changes?
The “Git Stash” command can stash the uncommitted code from the staged as well as unstaged changes. As In the below example, I have used “git stash -u” to stash the untracked changes. After Stashing the changes, Stashed changes will be reverted from the working area.
We can also pass comments while using the Stash command “git stash save <comment>" command.
2. How to view the existing stash list?
You can check the list of the stashes available in the git repository using the “git stash list”.
3. How to apply the existing stash?
Use the “git stash apply” command to apply the changes that were stored in the last stash. For Demonstration, I have below file structure before applying the stash.
Applying the stash using the “git stash apply” command. You can confirm the changes applied again with the git status command.
As you can see the "about.html" file which I stashed is applied again.
In case you want to apply a specific stash from the list, use “git stash apply <stash_id>”.
When we apply the stash with the “git stash apply” command, git stash changes will be applied to your working copy and keeps the stash as well in the stash list.
Use the “git stash pop” command which applies the last stash or particular stash if passed along with the stash id in the working area as well as removes the applied stash changes from the stash list.
4. How to delete a particular stash?
Use the “git stash drop” command to delete the most recent stash from the stash list or can also use to drop a stash with the stash id. For example, “git stash drop <stash_id>”. In the below image, I have demonstrated both approaches.
5. How to clear all existing stash?
Use the “git stash clear” command to clear the entire stash saved items.
6. How to create a branch with the stash?
If some changes (like bug fix etc.) need to be done in the git branch before applying the existing stash. It is recommended to create a branch with the stash so that in the future, reapplying the stash will not lead to conflicts.
“git stash branch <branch_name>”
7. How to use the git stash show command?
Use the “git stash show” command to view the difference before the stash and after the stash operations. Use “git stash show -p” where -p stands for the partial stash which provides the details for the stash’s edited files as well as the content.