Stashing is a fantastic way to temporarily set aside your current work and return to it later. Imagine you’re developing a new feature and suddenly need to fix a critical bug. First, add your changes to the index:
git add .
Alternatively, you can add individual files as needed. Once your changes are staged, stash them with:
git stash
Now, your working directory is clean, and you can focus on fixing the bug. After resolving the issue, you can retrieve your stashed work using:
git stash apply
Git allows you to create multiple stashes. To view all your stashes, use:
git stash list
If you need to apply a specific stash from the list, such as the second one, you can do so with:
git stash apply stash@{1}
To apply and simultaneously remove the top stash from the stack, use:
git stash pop
(Note: git stash pop removes the stash after applying it, whereas git stash apply does not.)
To manually delete a specific stash, use:
git stash drop
And if you want to clear all stashes, simply run:
git stash clear
Using these commands, you can efficiently manage your changes and switch contexts without losing your progress.