DEV Community

Cover image for What is Git Stash ? A Complete Guide by Ariful Haque Sajib
Ariful Haque Sajib
Ariful Haque Sajib

Posted on • Edited on

What is Git Stash ? A Complete Guide by Ariful Haque Sajib

Learn how to use Git Stash to save uncommitted changes, switch branches, and restore work efficiently. Essential commands & best practices explained.

Have you ever been in the middle of coding, needed to switch branches, but didn’t want to commit half-finished work? Git Stash is the solution!

This guide will explain:
✅ What Git Stash is and when to use it
✅ Essential Git Stash commands with examples
✅ Best practices and common pitfalls

What is Git Stash?
Git Stash temporarily saves your uncommitted changes (modified, staged, or untracked files) so you can switch branches, pull updates, or work on something else without losing progress.

When Should You Use Git Stash?
✔ Switching branches with uncommitted changes
✔ Saving work before pulling remote updates
✔ Temporarily storing experimental code
✔ Avoiding unnecessary “WIP” (Work in Progress) commits

Essential Git Stash Commands

1. Stash Your Changes
Save all modified and staged files (but not untracked files):

git stash
Enter fullscreen mode Exit fullscreen mode

Save including untracked files:

git stash -u
Enter fullscreen mode Exit fullscreen mode

Save with a custom message for easy identification:

git stash save "Feature X - WIP"
Enter fullscreen mode Exit fullscreen mode

2. List All Stashes
View your stash history:

git stash list
Enter fullscreen mode Exit fullscreen mode

Example output:

stash@{0}: On main: Feature X - WIP  
stash@{1}: On dev: Bugfix Y - partial
Enter fullscreen mode Exit fullscreen mode

3. Apply (Restore) a Stash
Restore the latest stash (keeps it in the stash list):

git stash apply
Enter fullscreen mode Exit fullscreen mode

Restore a specific stash (e.g., stash@{1}):

git stash apply stash@{1}
Enter fullscreen mode Exit fullscreen mode

4. Pop a Stash (Apply & Delete)
Restore the latest stash and remove it from the stash list:

git stash pop
Enter fullscreen mode Exit fullscreen mode
  1. Delete a Stash Remove a specific stash:
git stash drop stash@{1}
Enter fullscreen mode Exit fullscreen mode

Clear all stashes:

git stash clear
Enter fullscreen mode Exit fullscreen mode
  1. Create a Branch from a Stash If your stash conflicts with current changes, create a new branch:
git stash branch new-branch-name stash@{1}
Enter fullscreen mode Exit fullscreen mode

Conclusion
Git Stash is a powerful tool for managing unfinished work without cluttering your commit history. Whether you’re switching tasks, pulling updates, or experimenting, stash helps keep your workflow clean.

I’m Md Ariful Haque Sajib, a Software Engineer, specializing in PHP, Laravel, Spring Boot, Vue.js, and React.js. With expertise in E-commerce, Payment Gateways, APIs, various Management and Finance-based software. I am passionate about 🧠 learning and delivering 🌟 high-quality solutions.

📌 Follow me for updates and insights:
🌐 GitHub: ArifulHaque313
🔗 LinkedIn: Md Ariful Haque Sajib

📧 Contact: asajib7654@gmail.com

📢 Hashtags:

GitStash #Git #Github

ArifulHaqueSajib #arifulhaque313 #sajib

Hope this makes it more visually appealing! 😊

Top comments (0)