Stash
Introduction
Sometimes you must switch branches before finishing current edits—hotfix on main while feature code is half-done. Stash saves uncommitted work on a stack, gives you a clean working tree, and lets you reapply later with git stash pop.
Prerequisites
When to Stash
| Scenario | Stash? |
|---|---|
Urgent fix on main, feature WIP uncommitted | Yes |
| Pull blocked by local modifications | Often yes (or commit WIP) |
| Long-lived uncommitted work | Prefer feature branch + commits instead |
Stash is a temporary shelf—not a substitute for commits.
Basic Stash Workflow
Edit without committing:
echo "half done" >> feature.js
git statusSave and clear working tree:
git stash push -m "WIP feature.js experiment"Code explanation:
pushstores staged + unstaged tracked changes-madds readable message—helps when list grows
Switch branch and work:
git switch main
# fix bug, commit ...
git switch feature/loginRestore stash:
git stash popCode explanation:
popapplies latest stash and removes it from stack- Conflicts possible if same lines changed—resolve like merge
List and Apply Without Removing
git stash listExample output:
stash@{0}: On feature/login: WIP feature.js experiment
stash@{1}: WIP on main: temp debugApply but keep in list:
git stash apply stash@{1}Drop when done:
git stash drop stash@{1}Clear all stashes:
git stash clearStash Including Untracked Files
New files not yet tracked:
git stash push -u -m "Include new prototype files"Code explanation:
-u(--include-untracked) adds untracked files to stash
Stash Only Some Paths
git stash push -m "Only CSS tweaks" -- src/styles.cssOther modified files stay in working tree.
Stash vs Commit vs Branch
| stash | WIP commit | feature branch | |
|---|---|---|---|
| Visible in log | No | Yes | Yes |
| Share with team | No | Yes (push) | Yes |
| Best for | Minutes/hours pause | Savepoint on branch | Real feature work |
Tip
Name Your Stashes
Default message WIP on branch is vague—-m "reason" saves confusion after a week.
Stash and Pull
git stash
git pull origin main
git stash popResolve conflicts if pull touched same lines as stash.
Inspect Stash Contents
git stash show -p stash@{0}Same patch format as git diff.
FAQ
pop lost my stash?
Successful pop removes it—changes should be in working tree. Failed pop keeps stash.
Stash on wrong branch?
Stash is repo-wide; apply on any branch—watch for wrong-context conflicts.
How many stashes?
Unlimited stack—clean old ones with drop or clear.
Stash secrets?
Stash lives in .git—do not stash .env with credentials; add to .gitignore.
stash vs commit --amend?
Different jobs—stash pauses uncommitted work; amend fixes last commit.
IDE stash button?
VS Code / JetBrains wrap same commands—same behavior.