DEV Community

Sanchita Paul
Sanchita Paul

Posted on

Git Stash For Temporary Save

git stash 
Enter fullscreen mode Exit fullscreen mode

git stash is a Git command that allows you to temporarily save changes that are not yet committed to a branch, so you can switch to a different branch or perform other operations without committing the changes. The command creates a stash, which is a stack of saved changes that you can later apply or drop.

When you run git stash , Git saves your changes and reverts your working directory to the state of the last commit. You can then switch to a different branch or perform other operations without worrying about committing incomplete changes.

git stash apply
Enter fullscreen mode Exit fullscreen mode

To retrieve the saved changes, you can use the git stash apply command.

git stash pop
Enter fullscreen mode Exit fullscreen mode

If you want to apply the changes and remove them from the stash, you can use git stash pop.

git stash save -m "my message"
Enter fullscreen mode Exit fullscreen mode

You can also create a stash with a message by using the -m or --message option. This can be useful to help you remember why you stashed the changes in the first place.

git stash list
git stash drop
git stash clear
Enter fullscreen mode Exit fullscreen mode

In addition to git stash apply and git stash pop, there are several other commands you can use to manage stashes, such as git stash list to list all stashes, git stash drop to remove a stash from the stack, and git stash clear to remove all stashes.

git stash clear removes all the stashes from the stash stack, but it does not restore any changes that were previously stashed. If you had stashed changes before running git stash clear, then those stashes would have been deleted, but any changes that were already applied from the stash would still be present in your working directory.

If you want to restore your working directory to the state it was in before you applied the stash, you can use the git stash apply or git stash pop command to reapply the stash and restore your changes, and then either commit those changes or discard them as appropriate.

git stash apply stash@{n} 
Enter fullscreen mode Exit fullscreen mode

For example, you could run git stash pop to apply the most recent stash and remove it from the stash stack. If you want to apply a specific stash, you can use git stash apply stash@{n}, where n is the index of the stash you want to apply. You can use git stash list to see the list of available stashes and their indexes.

Top comments (0)