When you're working on a project, it’s very common to start coding something and suddenly realize you need to switch branches, fix a bug, or pull new changes. But your current work is incomplete and not ready to commit.
This is exactly where Git Stash becomes one of the most powerful tools in your workflow.
In this article, we will explore Git Stash in depth — from basics to advanced usage — with real-world scenarios and clear explanations.
🚀 What is Git Stash?
Git Stash temporarily saves your uncommitted changes (both staged and unstaged) so you can work on something else and come back later.
Think of it as a temporary storage (clipboard) for your code changes.
🧠 Why Use Git Stash?
Here are common scenarios:
- You are in the middle of work but need to switch branches
- You need to quickly fix a bug in production
- You want to pull latest changes but your working directory is dirty
- You want a clean working tree without committing incomplete work
📦 Basic Usage
1. Save Changes
git stash
This command:
- Saves your modified tracked files
- Reverts your working directory to the last commit
2. View Stashes
git stash list
Example output:
stash@{0}: WIP on main: 123abc Fix login bug
stash@{1}: WIP on feature: 456def Add new API
3. Apply Stash
git stash apply
This restores the latest stash but keeps it in the stash list.
4. Apply and Remove (Pop)
git stash pop
This:
- Applies the stash
- Removes it from the stash list
5. Drop a Stash
git stash drop stash@{0}
Deletes a specific stash.
6. Clear All Stashes
git stash clear
⚠️ Warning: This deletes all stashes permanently.
🔍 Stashing Specific Changes
Stash Only Staged Changes
git stash --staged
Stash Including Untracked Files
git stash -u
or
git stash --include-untracked
Stash Everything (Including Ignored Files)
git stash -a
🏷️ Naming Your Stash
Instead of default messages, you can name your stash:
git stash push -m "WIP: payment integration"
This helps a lot when managing multiple stashes.
🔄 Applying Specific Stash
git stash apply stash@{1}
🔀 Create Branch from Stash
One of the most useful features:
git stash branch new-branch-name
This:
- Creates a new branch
- Applies the stash
- Removes it from stash list
Perfect when your temporary work becomes important.
⚠️ Handling Conflicts
When applying a stash, conflicts may occur if:
- The same lines were changed
- The codebase has evolved
Git will mark conflicts just like a merge:
<<<<<<< HEAD
Your current code
=======
Stashed code
>>>>>>> stash
You must resolve these manually.
🧩 Internal Working (Advanced)
When you run:
git stash
Git actually creates:
- A commit for your working directory
- A commit for your index (staged changes)
These are stored in a special reference:
refs/stash
So stash is not magic — it's structured commits behind the scenes.
🔥 Real-World Workflow Example
Scenario:
You're working on a feature:
git checkout feature-auth
You modify files but suddenly:
👉 You must fix a bug in main
Solution:
git stash
git checkout main
# fix bug
git commit -m "hotfix: login issue"
git checkout feature-auth
git stash pop
You’re back exactly where you left off.
💡 Best Practices
- Use descriptive stash messages
- Avoid keeping too many stashes
- Prefer branches for long-term work
- Use
git stash popcarefully (it deletes stash)
❌ Common Mistakes
- Forgetting stash exists → losing track of work
- Using stash instead of proper commits
- Not naming stashes → confusion later
- Clearing stash accidentally
🧠 Pro Tips
- Use
git stash show -pto see full diff:
git stash show -p stash@{0}
- Stash only part of changes (interactive):
git stash -p
- Combine with rebase or pull for cleaner workflow
🎯 Conclusion
Git Stash is a powerful, flexible tool that helps you:
- Switch context quickly
- Keep your work safe
- Maintain a clean workflow
But remember:
👉 It’s a temporary solution, not a replacement for commits or branches.
Mastering Git Stash will significantly improve your productivity and confidence when working with Git.
Top comments (0)