Git Stash (Temporary Save)
What is Stash?
git stash temporarily saves uncommitted work so you can:
- Switch branches
- Pull latest code
- Fix urgent bugs
- Again come back to uncompleted branch and continue WITHOUT committing incomplete work.
🔥 Real-Time Example (Very Common)
You are working on CustomerController.java Suddenly your manager says “Production bug! Fix immediately! But your work is half done.
Step 1: Save work temporarily
git stash
✔ Working directory becomes clean
✔ Your changes are saved safely
Step 2: Fix urgent issue
git pull
git pull
git checkout main
# Fix bug
git push
git push
git commit -m "Fix prod issue"
Step 3: Restore your work
git checkout feature-branch
git stash pop
git stash pop
Your unfinished code is back 🎉
Stash Commands You MUST Know
git stash # Save changes
git stash list # View stashes
git stash apply # Apply stash (keep stash)
git stash pop # Apply + delete stash
git stash drop # Delete stash
git stash list # View stashes
git stash apply # Apply stash (keep stash)
git stash pop # Apply + delete stash
git stash drop # Delete stash
Stash with Message (Best Practice)
git stash push -m "WIP: customer validation"
git stash push -m "WIP: customer validation"
When to Use Stash
✅ Switching branches
✅ Pulling latest changes
✅ Urgent production fixes
❌ Long-term storage (commit instead)
0 Comments