What is Git stash and when should you use it
· Category: Git
Short answer
Git stash lets you temporarily save uncommitted changes so you can switch branches or pull updates without creating a messy commit. Use git stash push -m "message" to save and git stash pop to restore. If you need to bring a specific commit into your current work, see how to cherry-pick a commit in Git. For fixing commits before stashing, see how to amend a Git commit.
Steps
- Save current changes:
git stash push -m "partial login feature" - Switch to another branch or pull updates
- Return to your branch:
git checkout feature-branch - Restore changes:
git stash pop - View stash list:
git stash list
Tips
- Name your stashes with descriptive messages to keep track of multiple stashes
- Use
git stash applyinstead ofpopif you want to keep the stash in the list - If stashed changes conflict after a merge, see how to resolve Git merge conflicts