Git Stash & Pop: Managing Temporary Work

Git Stash & Pop: Managing Temporary Work
1. What is a Stash? (The Virtual Shelf)
A Stash is a place where Git stores your current changes without creating a permanent commit.
The Problem:
- You have 5 modified files.
- You need to checkout
mainto fix a bug. git checkout mainfails: "Your local changes to the following files would be overwritten..."
The Solution:
2. Returning to Your Work: Pop vs. Apply
Once you've finished the hotfix, go back to your branch and "Bring back" your changes.
git stash pop: Retrieves your changes and DELETES them from the stash list.git stash apply: Retrieves your changes but KEEPS a copy in the stash drawer. This is safer if you aren't sure if you'll run into conflicts.
3. Managing Multiple Stashes
If you have different sets of work saved, you can "Label" them.
You can then bring back a specific stash: git stash apply stash@{1}.
4. Stashing Untracked Files
By default, git stash only saves files that Git already knew about. If you created a new file, you must tell Git to include it.
Frequently Asked Questions
Does a Stash survive a computer restart?
Yes! A stash is stored in the .git folder on your hard drive. It will stay there until you either pop it or explicitly clear it.
Can I stash my changes and move them to a different branch?
YES! This is the #1 "Pro Use Case." If you accidentally start a feature on the main branch, just git stash, switch to a new feature-y branch, and git stash pop. Your work is now on the correct branch.
Key Takeaway
git stash is the "Wait, let me just..." button of version control. By mastering the stash and pop workflow, you never have to worry about "Messy WIP commits" or being unable to switch branches during an emergency. It's the ultimate tool for a productive, flexible developer.
Read next: Git Bisect: Finding Bugs with Binary Search →
Part of the GitHub Mastery Course — masters of the shelf.
