Git Stash to Save Temporary

Git Stash (Temporary Save)

What is Stash?
git stash temporarily saves uncommitted work so you can:

  1. Switch branches
  2. Pull latest code
  3. Fix urgent bugs
  4. 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 checkout main

# Fix bug
git push
git commit -m "Fix prod issue"

Step 3: Restore your work
git checkout feature-branch
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

Stash with Message (Best Practice)
git stash push -m "WIP: customer validation"


When to Use Stash

✅ Switching branches

✅ Pulling latest changes

✅ Urgent production fixes

❌ Long-term storage (commit instead)

Post a Comment

0 Comments