Stashing Changes
Stashing lets you temporarily shelve changes so you can work on something else, then come back to them later. It’s like putting your work in a drawer.
Creating a Stash
git stash
This saves your staged and unstaged changes and reverts your working directory to the last commit.
You can add a descriptive message:
git stash save "WIP: login form styling"
Without a message, Git generates a default one based on the branch and commit.
Managing Stashes
git stash list # See all stashed changes
git stash pop # Apply most recent stash and remove it
git stash apply # Apply without removing from list
git stash drop # Delete most recent stash
git stash clear # Remove all stashes
Stashes are stored in a stack. pop and apply work on the most recent one by default.
Viewing Stash Contents
git stash show # Quick summary of changed files
git stash show -p # Full diff of changes in the stash
git stash show stash@{2} # Show a specific stash
This helps you identify which stash contains the changes you need.
Stashing Specific Files
git stash push -m "only package.json changes" -- package.json
This stashes only the specified files, leaving everything else intact.
When to Use Stash
- Switching branches with uncommitted work
- Pulling changes when you have local modifications
- Quick context switches during debugging
- Experimenting without creating a commit
- Cleaning your working directory temporarily
Best Practices
- Use descriptive messages:
git stash save "add user avatar upload" - Don’t stash too many times — commit or discard instead
- Pop stashes promptly to avoid losing track of them
- Test after applying a stash to ensure nothing broke
- Keep your stash list small by using it as a temporary holding area
Common Mistakes
- Forgetting that
stash popcan cause merge conflicts - Leaving stashes unattended for too long
- Using
git stashwithout checkinggit stash listfirst - Assuming stash is a substitute for committing
- Not using
-mand ending up with hard-to-identify stashes