git stash — Temporarily Shelving Changes
How to stash uncommitted changes: git stash save, git stash pop, stashing untracked files (-u), viewing stash lists, and dropping stashes.
git stash — Temporarily Shelving Changes
The git stash command takes your uncommitted changes (both staged and unstaged) and saves them onto a dirty working directory stack, reverting your working tree back to a clean state matching the last commit (HEAD).
1. When to Use git stash
Use git stash when you are in the middle of writing a feature and need to immediately switch branches to fix an urgent bug without creating an incomplete "work in progress" commit.
2. Common Stash Commands
# 1. Save all tracked changes with an optional descriptive message:
git stash push -m "WIP: dark mode styling"
# 2. Include untracked files in the stash (-u):
git stash -u
# 3. View the list of saved stashes:
git stash list
# 4. Apply the most recent stash and remove it from the stack:
git stash pop
# 5. Apply a specific stash from the list without removing it:
git stash apply stash@{1}
# 6. Delete a specific stash:
git stash drop stash@{0}
# 7. Clear all saved stashes completely:
git stash clearRelated Topics
git rebase — Rewriting and Linearizing History
Mastering git rebase: standard rebase, interactive rebase (squash, reword, drop, edit), and the Golden Rule of Rebasing.
git reset — Undoing Commits Locally
Mastering git reset: soft vs mixed vs hard resets, moving HEAD pointers, rescuing lost commits via reflog, and dangerous pitfalls.