Files
mintlify__docs/git/stash.mdx
2026-02-09 20:46:44 +00:00

392 lines
5.3 KiB
Plaintext

---
title: git stash
description: Stash the changes in a dirty working directory away
---
Temporarily save changes without committing.
## Syntax
```bash
git stash [options] [<command>]
```
## Description
`git stash` saves your uncommitted changes (staged and unstaged) and reverts your working directory to match the HEAD commit. You can reapply stashed changes later.
## Common usage
Stash current changes:
```bash
git stash
```
Stash with a message:
```bash
git stash push -m "Work in progress on feature X"
```
Reapply stashed changes:
```bash
git stash pop
```
## Subcommands
### push
Save changes to stash:
```bash
git stash push -m "message"
```
### pop
Apply and remove most recent stash:
```bash
git stash pop
```
### apply
Apply stash without removing it:
```bash
git stash apply
```
### list
List all stashes:
```bash
git stash list
```
### show
Show changes in a stash:
```bash
git stash show
```
### drop
Delete a specific stash:
```bash
git stash drop stash@{0}
```
### clear
Delete all stashes:
```bash
git stash clear
```
## Options
<ParamField path="-u, --include-untracked" type="flag">
Include untracked files in stash
```bash
git stash -u
```
</ParamField>
<ParamField path="-a, --all" type="flag">
Include ignored files in stash
</ParamField>
<ParamField path="-m, --message" type="string">
Add description to stash
```bash
git stash push -m "Description"
```
</ParamField>
<ParamField path="-p, --patch" type="flag">
Interactively select changes to stash
</ParamField>
## Examples
### Basic stash
```bash
# Stash changes
git stash
# Do something else
git checkout other-branch
# Return and reapply
git checkout original-branch
git stash pop
```
### Stash with message
```bash
git stash push -m "Half-finished authentication feature"
```
### List stashes
```bash
git stash list
```
Output:
```
stash@{0}: On main: Half-finished authentication
stash@{1}: WIP on feature: Added validation
stash@{2}: On develop: Updated dependencies
```
### Apply specific stash
```bash
git stash apply stash@{1}
```
### View stash contents
```bash
# Summary
git stash show
# Full diff
git stash show -p
# Specific stash
git stash show stash@{1}
```
### Stash specific files
```bash
git stash push -m "message" path/to/file.txt
```
### Stash including untracked files
```bash
git stash -u
```
## Common workflows
### Switch branches with uncommitted work
```bash
git stash
git checkout other-branch
# Work on other branch
git checkout original-branch
git stash pop
```
### Save WIP before pulling
```bash
git stash
git pull
git stash pop
```
### Temporarily test something
```bash
# Save current work
git stash
# Test something clean
git checkout test-branch
# Restore work
git stash pop
```
### Multiple stashes
```bash
# Create multiple stashes
git stash push -m "Feature A work"
git stash push -m "Feature B work"
# List them
git stash list
# Apply specific one
git stash apply stash@{1}
```
## Stash vs Commit
### Use stash when:
- Changes are incomplete
- Quickly switch contexts
- Not ready to commit
- Testing something temporarily
### Use commit when:
- Changes are complete
- Want to preserve in history
- Ready to share with team
- Creating a logical checkpoint
## Advanced usage
### Create branch from stash
```bash
git stash branch new-branch-name stash@{0}
```
Creates a new branch and applies the stash.
### Interactive stash
```bash
git stash -p
```
Choose which changes to stash interactively.
### Stash only staged changes
```bash
git stash --staged
```
### Keep index when popping
```bash
git stash pop --index
```
Restores both working directory and staging area.
## Handling conflicts
If `git stash pop` causes conflicts:
```bash
# Fix conflicts in editor
git add resolved-file.txt
# Stash is not automatically dropped
# Drop it manually after resolving
git stash drop
```
## Best practices
- Use descriptive messages with `-m`
- Don't keep stashes for long periods
- Clean up old stashes regularly
- Use `git stash list` to track stashes
- Prefer commits over stashes for important work
- Apply stash with `apply` first to test, then `drop` if successful
## Viewing stash details
### List with dates
```bash
git stash list --date=local
```
### Show files in stash
```bash
git stash show --name-only
```
### Full diff of stash
```bash
git stash show -p stash@{0}
```
## Cleaning up stashes
### Drop specific stash
```bash
git stash drop stash@{1}
```
### Clear all stashes
```bash
git stash clear
```
<Warning>
`git stash clear` permanently deletes all stashes. Use with caution.
</Warning>
## Recovering dropped stash
Stashes are retained for a while even after dropping:
```bash
# Find lost stash
git fsck --no-reflog | grep commit
# View content
git show <commit-hash>
# Recover if needed
git stash apply <commit-hash>
```
## Troubleshooting
### Stash pop causes conflicts
```bash
# Resolve conflicts
# Then drop stash manually
git stash drop
```
### Can't stash
Ensure you're in a git repository:
```bash
git status
```
### Lost stash
Check reflog:
```bash
git reflog
```
Look for stash entries and recover with hash.
## Related commands
- [git commit](/git/commit) - Permanently save changes
- [git reset](/git/reset) - Undo changes
- [git checkout](/git/checkout) - Switch branches
- [git clean](/git/clean) - Remove untracked files