For All the Commands Below
The commands below assume you've navigated to the working directory of your Git repository. Ensure you're in the correct project folder before executing any stash operations to avoid unintended consequences.
Create a Stash
Git stash is your safety net when you need to quickly switch contexts without committing incomplete work. Whether you're responding to an urgent bug fix or switching branches mid-feature, stashing preserves your uncommitted changes in a temporary storage area. To stash your current work, use one of these commands:
- git stash
- git stash save "A meaningful name/message"
- NOTE: You can maintain multiple stashes simultaneously, creating a stack of saved states. While naming isn't mandatory, descriptive messages like "WIP: user authentication module" or "before refactoring payment logic" will save you time when working with multiple stashes.
Git Stash Command Options
| Feature | Basic Stash | Named Stash |
|---|---|---|
| Command | git stash | git stash save "message" |
| Organization | Harder to identify | Easy to identify |
| Best for | Quick temporary saves | Multiple ongoing changes |
Git allows you to create multiple stashes. While naming is optional, meaningful names help identify stashes later, especially when working on different features simultaneously.
List Stashes
As your stash collection grows, you'll need visibility into what you've stored. This becomes particularly valuable during long development cycles or when collaborating on complex features.
- git stash list
Understanding Stash List Output
Stash Index
Each stash gets a unique identifier like stash@{0}, stash@{1}. Lower numbers indicate more recent stashes.
Stash Message
Shows your custom message or default branch information. Helps identify the purpose of each stash.
Restore/Apply a Stash
When you're ready to resume your previous work, Git provides flexible options for applying stashed changes. The key distinction lies in whether you want to preserve the stash for future use or remove it once applied:
- git stash pop
- To apply a specific stash and remove it from your stash list, use the following command. Replace stash@{0} with your target stash identifier from the stash list:
- git stash pop stash@{0}
- To apply a specific stash while keeping it in your stash list for potential reuse, execute this command. Again, replace stash@{0} with your desired stash reference:
- git stash apply stash@{0}
Pop vs Apply Commands
| Feature | git stash pop | git stash apply |
|---|---|---|
| Stash removal | Removes from list | Keeps in list |
| Use case | One-time application | Multiple applications |
| Risk level | Higher - stash deleted | Lower - stash preserved |
Applying Specific Stashes
List Available Stashes
Run git stash list to see all available stashes with their identifiers
Identify Target Stash
Note the stash identifier (e.g., stash@{0}, stash@{1}) for the changes you want
Apply the Stash
Use git stash pop stash@{0} or git stash apply stash@{0} with your specific identifier
Removing Stashes
Stash maintenance is crucial for keeping your repository clean and avoiding confusion. Over time, accumulated stashes can become noise, making it harder to locate relevant saved work.
- git stash clear
To delete a specific stash while preserving others, use the targeted removal command. Replace stash@{0} with the exact stash identifier you want to eliminate:
- git stash drop stash@{0}
Git Stash Clear vs Drop
Both git stash clear and git stash drop permanently delete stashed changes. Make sure you no longer need the stashed code before removing it.
Go Beyond Git
Mastering Git is just the beginning of your development journey. Whether you're looking to advance your current role or pivot into tech, our comprehensive curriculum covers the full spectrum of modern development skills:
Available NYC Coding Programs
Web Development School
Comprehensive full-stack development training. Learn front-end and back-end technologies in hands-on, expert-led classes.
Python Bootcamp
Intensive Python programming course covering fundamentals to advanced topics. Small class sizes ensure personalized attention.
Data Science School
Master data analysis, machine learning, and statistical modeling. Practical projects with real-world datasets and expert instruction.
Web Design School
Learn modern design principles, user experience, and visual design. Hands-on training with industry-standard tools and techniques.