Skip to content

Stashing Work in Progress ​

You are in the middle of editing three files when your teammate asks you to review their PR on another branch. You cannot commit half-finished work. You cannot just switch branches because Git will refuse with uncommitted changes that conflict. git stash shelves your changes, cleans your working tree, and lets you come back to them later.

What stash does ​

Stash takes the diff between your working tree (and optionally staged changes) and the current commit, packages it into a temporary commit-like object, and resets your working tree to match the commit. Later you apply that package back.

Before stash:
  Working tree:   edited files
  HEAD:           commit abc123
  
git stash:
  Working tree:   clean (matches HEAD)
  Stash entry:    saved diff -> refs/stash
  HEAD:           commit abc123 (unchanged)

After stash pop:
  Working tree:   edited files restored
  Stash entry:    removed from stash list

Step by step ​

1. Save your work ​

bash
git stash

Output:

Saved working directory and index state WIP on main: a1b2c3d fix header alignment

Your working tree is now clean. git status shows nothing modified.

2. Do something else ​

Switch branches, review a PR, fix a bug, commit. Stash stays out of your way.

bash
git switch review/feature-login
# review, merge, whatever
git switch main

3. Bring your work back ​

bash
git stash pop

Output:

Auto-merging src/app.js
On branch main
Changes not staged for commit:
  modified:   src/app.js
  modified:   src/utils.js

Dropped refs/stash@{0} (abc123def456...)

Your changes are back. The stash entry is gone (pop = apply + drop).

4. List saved stashes ​

You can have multiple stashes saved at once. They stack like a LIFO list:

bash
git stash list
stash@{0}: WIP on main: a1b2c3d fix header alignment
stash@{1}: WIP on feature-search: b2c3d4e add search placeholder

stash@{0} is the most recent. Each entry is a reference in .git/refs/stash and its reflog.

5. Apply without removing ​

If you want the same stash on two branches, or you are not ready to commit to dropping it:

bash
git stash apply stash@{1}

This restores the stash but keeps it in the list. Useful when you want to re-apply the same changes to a different branch.

6. Stash only some files ​

Sometimes you want to stash only part of what you changed:

bash
git stash push -- src/app.js

Only changes to src/app.js are stashed. Everything else stays modified.

Use pathspec patterns to exclude:

bash
git stash push -- ':(exclude)*config*'

7. Include untracked files ​

By default stash ignores files Git does not track. To include them:

bash
git stash -u

The -u flag includes untracked files. -a also includes ignored files.

8. Name your stash for clarity ​

bash
git stash push -m "half-finished API client"

git stash list now shows:

stash@{0}: On main: half-finished API client

When to use stash ​

  • Context switch. Mid-work and need to switch branches. Stash, switch, do the thing, switch back, pop.
  • Clean build. CI needs a clean tree. Stash, build, pop.
  • Test a theory. Apply a stash to a different branch to see if the same changes work there.
  • Squash work in progress. Stash, pull the latest main, stash pop, fix conflicts, commit cleanly.

When not to use stash ​

  • Branch-level work. If the changes will take more than a day, commit to a feature branch instead. A commit is permanent, documented, and pushable. A stash lives only on your machine and is easy to lose.
  • Collaboration. Stashes are local. You cannot push a stash. If someone else needs your changes, commit them to a branch.
  • Long-term storage. Stashes expire from the reflog after 30 days by default. Work that matters longer than that belongs in a commit.
  • Large number of changes. A stash with 50 modified files is hard to inspect and prone to conflicts when popping. Commit early, commit often.

Conflicts when popping ​

If the files you stashed have changed in the meantime, git stash pop can produce merge conflicts:

bash
git stash pop
# Auto-merging src/app.js
# CONFLICT (content): Merge conflict in src/app.js

The stash entry is NOT dropped when a conflict occurs (unlike a clean pop). This is deliberate: Git keeps the stash so you can retry after resolving.

Resolve conflicts normally:

bash
# Edit src/app.js to resolve markers
git add src/app.js
git stash drop    # Remove the stash now that it is applied

Or abort and try a different approach:

bash
git reset --hard HEAD
git stash apply   # Try again, maybe on a different branch

Quick reference ​

bash
git stash                      # Save all tracked changes
git stash push -m "message"    # Save with a descriptive name
git stash -u                   # Save including untracked files
git stash -a                   # Save including ignored files
git stash push -- <path>       # Save only specific files
git stash list                 # List saved stashes
git stash show -p stash@{n}    # Show what a stash contains
git stash pop                  # Apply most recent stash and remove it
git stash apply                # Apply without removing
git stash apply stash@{2}      # Apply a specific stash
git stash drop stash@{1}       # Delete a specific stash
git stash clear                # Delete all stashes
git stash branch <name>        # Create a branch from a stash

Next steps ​

Stashing is a quick way to save context, but for longer-lived work you want feature branches and commits. Read the guide on interactive rebase to learn how to clean up your commit history before pushing. Then try fixup and autosquash for editing history without manual rebase.

Git Master — Git Reference for Every Skill Level