Stashing Changes
git stash shelves uncommitted changes so you can switch context and restore them later.
Syntax
git stash
git stash popgit stash tucks away your uncommitted changes and gives you a clean working directory. It is perfect when you must quickly switch branches but are not ready to commit.
git stash— shelve current changes.git stash list— see stashed entries.git stash pop— reapply the most recent stash and remove it.git stash apply— reapply but keep the stash.
Example
# Mid-change, an urgent fix is needed elsewhere
git stash
git switch main # now clean
# ...handle the urgent task...
git switch feature/search-bar
git stash pop # get your work backWhen to use it
- A developer stashes unfinished work before switching to a hotfix branch, then pops the stash to resume afterward.
- A developer stashes local edits before running git pull to avoid a merge conflict with incoming changes.
- A developer stashes a half-complete feature so they can quickly demo the current stable state to a stakeholder.
More examples
Stash work and switch branches
Pushes a named stash including untracked files (-u), switches context, then restores the stash to resume work.
# Save all tracked and new files
git stash push -u -m "half-done payment form"
git switch hotfix/login-crash
# Fix the bug, commit, merge...
git switch feature/payments
git stash popList and inspect stashes
Lists all stashes with their index and shows the full diff of a specific stash for inspection before popping.
git stash list
# stash@{0}: On feature/payments: half-done payment form
# stash@{1}: WIP on main: 3d0e11f Refactor DB pool
# See what a stash contains
git stash show -p stash@{0}Apply a stash without dropping it
apply restores the stash to the working tree but leaves the stash entry intact, giving you a safety net before you decide to drop it.
# Apply but keep the stash entry
git stash apply stash@{1}
# Manually drop it when done
git stash drop stash@{1}
Discussion