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

ScenarioStash?
Urgent fix on main, feature WIP uncommittedYes
Pull blocked by local modificationsOften yes (or commit WIP)
Long-lived uncommitted workPrefer feature branch + commits instead

Stash is a temporary shelf—not a substitute for commits.

Basic Stash Workflow

Edit without committing:

bash
echo "half done" >> feature.js
git status

Save and clear working tree:

bash
git stash push -m "WIP feature.js experiment"

Code explanation:

  • push stores staged + unstaged tracked changes
  • -m adds readable message—helps when list grows

Switch branch and work:

bash
git switch main
# fix bug, commit ...
git switch feature/login

Restore stash:

bash
git stash pop

Code explanation:

  • pop applies latest stash and removes it from stack
  • Conflicts possible if same lines changed—resolve like merge

List and Apply Without Removing

bash
git stash list

Example output:

text
stash@{0}: On feature/login: WIP feature.js experiment
stash@{1}: WIP on main: temp debug

Apply but keep in list:

bash
git stash apply stash@{1}

Drop when done:

bash
git stash drop stash@{1}

Clear all stashes:

bash
git stash clear

Stash Including Untracked Files

New files not yet tracked:

bash
git stash push -u -m "Include new prototype files"

Code explanation:

  • -u (--include-untracked) adds untracked files to stash

Stash Only Some Paths

bash
git stash push -m "Only CSS tweaks" -- src/styles.css

Other modified files stay in working tree.

Stash vs Commit vs Branch

stashWIP commitfeature branch
Visible in logNoYesYes
Share with teamNoYes (push)Yes
Best forMinutes/hours pauseSavepoint on branchReal feature work

Tip

Name Your Stashes

Default message WIP on branch is vague—-m "reason" saves confusion after a week.

Stash and Pull

bash
git stash
git pull origin main
git stash pop

Resolve conflicts if pull touched same lines as stash.

Inspect Stash Contents

bash
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.