💡Learned git stash the Practical Way

Abhinav KumarAbhinav Kumar
2 min read

The Mistake 🧯

So today at my internship, I jumped into a bug fix task — but completely forgot the basic Git rule:
Always create a new branch from updated main before starting work.

What I did instead:
I started working directly on an older branch that was meant for some previous task.

Why is that bad?

  • Your current work mixes up with older unrelated changes.

  • It becomes hard to track what's what.

  • If you push, you might mess up the history or reopen an already-closed task.

  • Worst: someone reviewing your PR will be totally confused.


How I Fixed It 🔧 (With git stash)

I knew about git stash in theory but never used it practically. So, I asked my mentor to walk me through it. Here’s the exact flow we followed:


🛠 Git Fix Flow

  1. Stash your current changes
    (from project root or wherever inside the repo)

     git stash
    

    ⏳ This temporarily stores your uncommitted changes in a hidden stack. Your working directory is now clean.

  2. Switch to main & update it

     git checkout main
     git pull origin main
    

    🧠 Always update main so your new branch is based on the latest code.

  3. Create and switch to a new branch

     git checkout -b bugfix/fix-some-issue
    
  4. Apply your stashed changes

     git stash pop
    

    💥 This brings back your changes into the current (correct) branch.

  5. Stage, commit, and push

     git add .
     git commit -m "Fix: corrected logic for XYZ"
     git push origin bugfix/fix-some-issue
    

    ⚠️ Don’t accidentally type main when pushing. I did that once — wrong branch, wrong push.


🧠 What I Learned

  • Never skip the “create new branch from updated main” step — it's not optional.

  • git stash is a lifesaver for moving changes safely between branches.

  • Typing git push origin main when you're on a different branch can cause serious headaches 😅


🧾 Quick Recap: git stash Flow

# Save your work
git stash

# Switch and update main
git checkout main
git pull origin main

# Create and switches to a new branch
git checkout -b your-branch-name

# Bring back your changes
git stash pop

# Stage, commit, and push
git add .
git commit -m "Your message"
git push origin your-branch-name

That's it. Simple mistake, useful recovery — and now it’s burned into memory 🔥
Hope this helps someone avoid the same!


1
Subscribe to my newsletter

Read articles from Abhinav Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Abhinav Kumar
Abhinav Kumar

🎓 B.Tech CSE | 👨‍💻 Learning DSA & C++ | 🚀 Building projects & writing what I learn | 📚 Currently solving LeetCode & exploring Git/GitHub