Version ControlFebruary 10, 20252 min read

Git Stash Explained

Master git stash to save and restore uncommitted changes. Learn stash basics, advanced techniques, and practical workflows.

gitversion-controldevtools

Git Stash Explained

git stash is one of those commands that, once you learn it, you'll wonder how you ever lived without it. It lets you temporarily shelve uncommitted changes so you can work on something else, then come back and restore them later.

The Basic Workflow

# Save your current changes
git stash
 
# Do other work...
 
# Restore your stashed changes
git stash pop

Why Use Git Stash?

Common scenarios where git stash saves the day:

  • Switching branches — You're mid-feature but need to fix a bug on another branch
  • Pulling updates — You want to pull the latest changes but have local modifications
  • Experimenting — You want to try something without committing your current work

Stash with a Message

Give your stashes meaningful names:

git stash push -m "WIP: user authentication form"

Listing Stashes

See all your stashed changes:

git stash list

Output:

stash@{0}: On main: WIP: user authentication form
stash@{1}: On main: experimenting with new layout

Applying vs Popping

  • git stash pop — Applies and removes the stash
  • git stash apply — Applies but keeps the stash
# Apply without removing
git stash apply stash@{1}
 
# Apply and remove
git stash pop stash@{0}

Stashing Specific Files

# Stash only staged changes
git stash push --staged
 
# Stash specific files
git stash push -m "stash only these" path/to/file.js

Cleaning Up

# Drop a specific stash
git stash drop stash@{0}
 
# Clear all stashes
git stash clear

Best Practices

  1. Always use messages — Future you will thank present you
  2. Don't let stashes pile up — They're meant to be temporary
  3. Use branches for longer work — If you need to stash for more than a day, consider a branch instead

Git stash is a powerful tool in your version control toolkit. Use it wisely and it will keep your workflow clean and flexible.