📚 Advanced Git & GitHub Topics
Estimated reading: 5 minutes 16 views

🧳 Git Stash – Save Work-in-Progress Without Committing


🧲 Introduction – Why Use Git Stash?

Ever found yourself in the middle of work when you suddenly need to switch branches or pull the latest changes—but you’re not ready to commit your code yet? That’s where git stash comes in.

git stash allows you to temporarily save your changes (both staged and unstaged) and revert your working directory to a clean state—so you can work on something else and come back later.

🎯 In this guide, you’ll learn:

  • What git stash is and how it works
  • How to stash, view, apply, and drop changes
  • Use cases, common mistakes, and best practices

🔍 What Is Git Stash?

git stash temporarily shelves (or stashes) changes you’ve made to your working directory so you can work on something else, then come back and re-apply them later.

It’s like saving your progress without committing it.


🧰 Basic Git Stash Workflow

🛠️ Git Stash your current changes:

git stash

This saves:

  • Modified tracked files
  • Staged files

Your working directory becomes clean.


🔍 View list of Git stashes:

git stash list

Output:

stash@{0}: WIP on main: 2345abc Added login logic
stash@{1}: WIP on main: 89def12 Update footer

📦 Apply a Git stash:

git stash apply

This applies the latest stash without removing it from the stash list.

You can also apply a specific one:

git stash apply stash@{1}

🗑️ Drop a Git stash:

git stash drop stash@{0}

Removes a specific stash entry from the list.


🧹 Apply and remove the Git stash in one go:

git stash pop

Same as apply, but also removes the stash after applying it.


🧪 Example Use Case – Hotfix While Coding

You’re halfway into a feature and suddenly need to fix a bug on the main branch.

✅ Hotfix While Coding Steps:

git stash                 # Save your WIP changes
git checkout main         # Switch to main branch
git pull                  # Pull the latest changes
# Fix and commit the bug
git checkout feature      # Switch back
git stash pop             # Re-apply your changes

You didn’t need to commit incomplete work just to switch branches. That’s the power of git stash.


🧠 Advanced Git Stash Options

➕ Include untracked files:

git stash -u
# or
git stash --include-untracked

💥 Include everything (even ignored files):

git stash -a
# or
git stash --all

📝 Add a custom message:

git stash save "WIP: working on navbar logic"

🧱 Apply Stash to a Different Branch

You can even stash on one branch and apply it to another.

git stash
git checkout new-feature
git stash pop

✅ Just ensure the code you’re applying doesn’t conflict too heavily with the target branch.


🧩 GUI Alternatives for Git Stash

Popular Git GUI tools support stash actions visually:

  • VS Code Git Panel: Right-click → “Stash Changes”
  • Sourcetree: Has a dedicated Stash tab
  • GitKraken: Allows stash creation, naming, and restoration via UI

⚠️ Common Mistakes to Avoid

  • ❌ Forgetting that git stash doesn’t stash untracked files by default
  • ❌ Using stash pop without checking for conflicts
  • ❌ Losing a stash by forgetting to name it or apply it
  • ❌ Assuming stash is automatically applied to the original branch

✅ Best Practices for Using Git Stash

  • Use git stash -u if you’ve created new files but haven’t committed them.
  • Name your stashes for clarity (git stash save "fixing auth bug").
  • Clean up old stashes regularly using git stash drop or git stash clear.
  • Avoid using stash as a long-term solution—prefer commits for long-term tracking.

📊 Git Stash Command Summary Table

CommandDescription
git stashStashes modified/staged changes
git stash -uStashes including untracked files
git stash listLists all current stashes
git stash apply [stash@{n}]Applies a specific stash
git stash popApplies and deletes the latest stash
git stash drop stash@{n}Deletes a specific stash
git stash clearClears all stashes
git stash save "message"Stashes with a custom message

📌 Summary – Git Stash

git stash is a powerful utility when you’re caught between unfinished work and urgent Git operations. Whether switching branches, updating code, or collaborating on critical fixes, stash ensures you don’t lose your progress.

🔍 Key Takeaways:

  • Use git stash to save uncommitted changes temporarily.
  • Apply changes later with git stash apply or pop.
  • Combine with -u or -a to stash untracked and ignored files.

⚙️ Real-World Relevance: Developers frequently use stash during hotfixes, code reviews, and sprint switches without breaking momentum.


❓ Git Stash — Frequently Asked Questions (FAQs)

❓ Does git stash save untracked files?
✅ No, not by default. Use git stash -u to include them, or git stash -a to include even ignored files.


❓ What happens if I stash multiple times?
✅ Each stash is stored as a separate entry in a stack. You can view them with git stash list and apply them individually.


❓ How do I name a stash for easy reference?
✅ Use:

git stash save "WIP: navbar update"

This label appears in the stash list.


❓ Can I stash changes and switch branches safely?
✅ Yes. That’s the main use of stash! Stash, switch, do your work, then apply or pop the changes back.


❓ How do I delete a specific stash?
✅ Use:

git stash drop stash@{2}

❓ Is stashed work ever automatically restored?
✅ No. You must use git stash apply or git stash pop to restore it.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Git Stash

Or Copy Link

CONTENTS
Scroll to Top