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

  1. Save current changes: git stash push -m "partial login feature"
  2. Switch to another branch or pull updates
  3. Return to your branch: git checkout feature-branch
  4. Restore changes: git stash pop
  5. View stash list: git stash list

Tips

  • Name your stashes with descriptive messages to keep track of multiple stashes
  • Use git stash apply instead of pop if you want to keep the stash in the list
  • If stashed changes conflict after a merge, see how to resolve Git merge conflicts