Problem
- Imagine a Scenario where you are on the master branch and you make a new branch and switch to it
- You do some new work but don’t make any commits. What happens when you switch back to master?
- My changes come with me to the destination branch
- Git won't let me switch if it detects potential conflicts so it advises us to either commit or stash them
Stashing
- Git provides an easy way of stashing these uncommitted changes so that we can return to them later, without having to make unnecessary commits.
-
git stash
is super useful command that helps you save changes that you are not yet ready to commit. You can stash changes and then come back to them later. Now that you have stashed my changes, you can switch branches, create new commits, etc. - Running git stash will take all uncommitted changes (staged and unstaged) and stash them, reverting the changes in your working copy. We don’t see the changes but they are saved.
- Use
git stash pop
to remove the most recently stashed changes in your stash and re-apply them to your working copy. - You can use
git stash apply
to apply whatever is stashed away, without removing it from the stash. This can be useful if you want to apply stashed changes to multiple branches
Stashing Multiple Times
- You can add multiple stashes onto the stack of stashes. They will all be stashed in the order you added them.
- Run
git stash list
to view all stashes - Git assumes you want to apply the most recent stash when you run
git stash apply
, but you can also specify a particular stash likegit stash apply <stash-id>
- To delete a particular stash, you can use
git stash drop <stash-id>
- To clear out all stashes, run
git stash clear
Top comments (0)