10 minlesson

Git Stashing

Git Stashing

Stashing saves your uncommitted changes to a stack, letting you switch contexts quickly without committing work-in-progress.

Why Stash?

You're working on a feature when something urgent comes up:

bash
1$ git status
2Changes not staged for commit:
3 modified: feature.js
4
5$ git checkout main
6error: Your local changes to the following files would be overwritten by checkout:
7 feature.js
8Please commit your changes or stash them before you switch branches.

You're not ready to commit. Stashing saves the day.

Basic Stashing

Stash Current Work

bash
1$ git stash
2Saved working directory and index state WIP on feature/login: abc1234 Add login form
3
4$ git status
5On branch feature/login
6nothing to commit, working tree clean

Your changes are safely stored. Now you can switch branches.

Stash with Message

bash
1$ git stash push -m "WIP: login validation logic"
2Saved working directory and index state On feature/login: WIP: login validation logic

Messages help identify stashes later.

Viewing Stashes

List All Stashes

bash
1$ git stash list
2stash@{0}: On feature/login: WIP: login validation logic
3stash@{1}: WIP on main: def5678 Update deps
4stash@{2}: On feature/api: WIP: API refactor

Stashes are numbered like a stack. stash@{0} is the most recent.

Show Stash Contents

bash
1# Most recent stash
2$ git stash show
3 feature.js | 15 +++++++++------
4 1 file changed, 9 insertions(+), 6 deletions(-)
5
6# Show diff
7$ git stash show -p
8
9# Specific stash
10$ git stash show stash@{2}

Restoring Stashed Work

Apply (Keep Stash)

bash
1$ git stash apply
2On branch feature/login
3Changes not staged for commit:
4 modified: feature.js

The changes are restored, but the stash remains in the list.

Pop (Apply and Remove)

bash
1$ git stash pop
2On branch feature/login
3Changes not staged for commit:
4 modified: feature.js
5
6Dropped refs/stash@{0} (abc123...)

The stash is applied and removed from the list.

Apply Specific Stash

bash
1$ git stash apply stash@{2}

Selective Stashing

Stash Specific Files

bash
1$ git stash push -m "just feature.js" feature.js

Interactive Stashing

Choose which hunks to stash:

bash
1$ git stash push -p

Similar to git add -p, you'll be prompted for each change.

Include Untracked Files

By default, stash ignores untracked files:

bash
1$ git stash push -u -m "include new files"
2# or
3$ git stash push --include-untracked

Include Ignored Files Too

bash
1$ git stash push -a -m "everything including ignored"
2# or
3$ git stash push --all

Managing Stashes

Delete a Stash

bash
1$ git stash drop stash@{1}
2Dropped stash@{1} (abc123...)

Clear All Stashes

bash
1$ git stash clear

Warning: This is permanent! All stashes are deleted.

Create Branch from Stash

If a stash causes conflicts when applying, create a branch:

bash
1$ git stash branch new-feature-branch stash@{0}
2Switched to a new branch 'new-feature-branch'
3On branch new-feature-branch
4Changes not staged for commit:
5 modified: feature.js
6
7Dropped refs/stash@{0} (abc123...)

This creates a new branch at the commit where you stashed, applies the stash, and drops it.

Common Workflows

Quick Context Switch

bash
1# Save current work
2$ git stash push -m "WIP: feature X"
3
4# Handle urgent task
5$ git checkout main
6$ git checkout -b hotfix/urgent
7# ... fix bug ...
8$ git commit -am "fix: urgent bug"
9$ git push
10
11# Return to feature
12$ git checkout feature/x
13$ git stash pop

Review Changes Before Stashing

bash
1# Check what you're about to stash
2$ git diff
3
4# Stash it
5$ git stash push -m "checked and stashed"

Stash While Rebasing

If a rebase conflicts with uncommitted changes:

bash
1$ git stash
2$ git rebase main
3$ git stash pop

Stash Pitfalls

Stash Conflicts

If your codebase changed significantly, applying a stash may conflict:

bash
1$ git stash pop
2Auto-merging feature.js
3CONFLICT (content): Merge conflict in feature.js

Resolve the conflict, then the stash won't auto-drop. Clean up manually:

bash
1$ git stash drop

Lost Stashes

Stashes can be lost if you:

  • Run git stash clear
  • Drop them accidentally
  • Never apply before they expire (rare)

Tip: For important work, commit to a branch instead of stashing long-term.

Commands Summary

CommandPurpose
git stashStash changes (auto message)
git stash push -m "msg"Stash with message
git stash push -uInclude untracked files
git stash listList all stashes
git stash showShow stash summary
git stash show -pShow stash diff
git stash applyApply, keep stash
git stash popApply and remove stash
git stash dropDelete a stash
git stash clearDelete all stashes
git stash branch <name>Create branch from stash

Key Takeaway

Stashing is for quick context switches—minutes, not days. For longer parallel work, use worktrees or proper branches. Keep stash messages descriptive, and don't let stashes pile up.