Git Stash
Git Stash is your "Temporary Workspace". It allows you to safely set aside your current uncommitted changes so you can switch contexts (e.g., to fix an emergency bug) and return to your work later.
📦 The Stash Concept
Think of the stash as a specialized stack where you can "park" dirty working directory state:
- Temporary: Not meant for long-term storage.
- Branch-Agnostic: You can stash changes on one branch and apply them to another.
- History-Safe: Stashing does not create permanent commits in your timeline.
1. Saving Your Work
To save your current changes and revert to a clean working state:
# Basic stash
git stash
# Stash with a descriptive message (Recommended)
git stash push -m "work in progress: sidebar-v2"
# Stash including new (untracked) files
git stash -u2. Inspecting the Stack
You can have multiple stashes saved at once. They are stored in a LIFO (Last-In, First-Out) stack.
# View all stashed work
git stash list
# View the contents of the latest stash
git stash show3. Restoring Changes
When you are ready to resume your work, you have two main options:
git stash pop: Restores the latest changes and removes them from the stash stack.git stash apply: Restores the latest changes but keeps them in the stash stack (safer if you want to apply to multiple branches).
# Restore specific stash by index
git stash apply stash@{1}4. Cleaning Up
To keep your repo healthy, clean up stashes you no longer need:
# Delete the most recent stash
git stash drop
# Clear all stashes permanently
git stash clear5. Professional Use Case: Emergency Hotfix
[!TIP] Stash to Branch If you've stashed work and then find that the production branch has changed too much, avoid a merge conflict by creating a new branch directly from your stash:
git stash branch new-feature-name stash@{0}