Stashing Basics
What is Git Stash?
Git stash is a powerful feature that allows developers to temporarily save uncommitted changes without committing them to the repository. It provides a clean way to switch contexts or branches while preserving your current work.
Basic Stash Commands
Creating a Stash
## Stash current changes
git stash
## Stash with a descriptive message
git stash save "Work in progress: feature implementation"
Viewing Stashes
## List all stashes
git stash list
Stash Workflow
graph TD
A[Working Directory] --> B[Uncommitted Changes]
B --> C{Stash Changes?}
C -->|Yes| D[git stash]
D --> E[Clean Working Directory]
C -->|No| F[Continue Working]
Stash Operations
Command |
Description |
git stash |
Save current changes |
git stash list |
Show all stashes |
git stash apply |
Reapply most recent stash |
git stash pop |
Apply and remove most recent stash |
git stash drop |
Remove most recent stash |
Advanced Stash Techniques
Applying Specific Stashes
## Apply a specific stash
git stash apply stash@{n}
## Pop a specific stash
git stash pop stash@{n}
Stashing Untracked Files
## Include untracked files in stash
git stash -u
Common Use Cases
- Switching branches mid-work
- Pausing current task
- Cleaning working directory
- Preserving experimental changes
LabEx Tip
LabEx recommends using stash as a flexible tool for managing temporary code changes and maintaining a clean workspace.