Today I’ve learned a great Git feature that allows me to save my changes in the current branch so that I can switch to another one and make changes there.
Another useful situation is when we pull the changes from the remote branch while those changes conflict with our local changes. Instead of fixing the conflict or reverting back the changes, git stash allows me to save my changes so that I can revert back the changes and pull the changes from the remote repository. After the pull completes, we can use the git stash pop to load the changes that I have saved before. This is super useful.
Below is an example of the case:
- Save changes to branch A.
- Run git stash.
- Check out branch B.
- Fix the bug in branch B.
- Commit and (optionally) push to remote.
- Check out branch A.
- Run git stash pop to get your stashed changes back.