๐งณ Git Stash
git stash temporarily stores uncommitted changes so you can switch branches or work on something else without committing unfinished work.
๐ Who This Is For
- Beginners learning practical Git workflows
- Intermediate users managing multiple tasks
- Advanced engineers needing structured reference
- DevOps, sysadmins and developers working with complex branching models
๐งฉ What Stash Does
- Saves tracked changes (modified files)
- Optionally saves untracked files
- Clears your working directory
- Lets you restore changes later
Itโs like a โclipboardโ for your working directory.
๐ง Basic Usage
Save current changes
This stashes:
- modified tracked files
- staged files
๐ง Restore changes
This:
- applies the stash
- removes it from the stash list
If you want to keep the stash:
๐ง List stashes
Example output:
| stash@{0}: WIP on feature/login
stash@{1}: WIP on main
|
๐ง Stash with message
| git stash push -m "Fixing login bug"
|
๐ง Stash untracked files
By default, untracked files are NOT included.
To include them:
To include ignored files too:
๐ง Show stash contents
| git stash show
git stash show -p
|
๐ง Apply a specific stash
| git stash apply stash@{2}
|
๐ง Drop a stash
๐ง Clear all stashes
๐ง When to Use Stash
- You need to switch branches quickly
- You must pull latest changes but have local modifications
- Youโre interrupted by a hotfix
- You want to test something without committing
- Youโre in the middle of a rebase and need to save work
โ ๏ธ Common Pitfalls
- Stash does not include untracked files unless
-u is used
- Stash can cause conflicts when popping
- Stash is local only (not shared across machines)
- Stash can be lost if you clear it accidentally
๐งฉ Best Practices
- Use messages (
-m) to avoid confusion
- Avoid keeping many stashes โ they become unmanageable
- Prefer
apply over pop when unsure
- Use stash before risky operations (rebase, reset)