Skip to main content

Git stash Guide

When to Use

When you're mid-task and need to temporarily switch branches, handle an urgent issue, or simply don't want to commit a batch of half-finished changes, you can stash the current state.

Check Current State First

git status --short
git stash list
  • git status --short -- confirm what changes currently exist.
  • git stash list -- confirm what stashes are already saved in the repository.
ScenarioRecommended CommandResult
Stash tracked changesgit stash push -m "wip: message"Save current state and restore clean working directory
Stash including untracked filesgit stash push -u -m "wip: message"Also includes new files
View stash listgit stash listSee all saved stashes
View what a stash changedgit stash show -p stash@{0}Show the diff
Restore and delete stashgit stash popAuto-removed after successful application
Restore but keep stashgit stash apply stash@{0}Convenient for repeated attempts
Delete a stashgit stash drop stash@{0}Manually clean up unused stashes

Common Scenarios

Temporarily Switch to Another Branch

git stash push -m "wip: refactor auth"
git switch main

Restore when you come back:

git switch feature/auth
git stash pop

Stash Untracked Files Too

git stash push -u -m "wip: include new config"

By default, git stash does not include untracked files; use -u to include them.

Check What's in the Stash Before Restoring

git stash list
git stash show -p stash@{0}
git stash apply stash@{0}

Create a New Branch Directly from a Stash

git stash branch rescue-work stash@{0}

Useful when the stash and current branch have diverged significantly and you don't want to restore directly on the original branch.

Risks and Boundaries

  • git stash only saves changes to tracked files by default; ignored files require -a, untracked files require -u.
  • git stash pop only deletes the stash when application succeeds; if a conflict occurs, the stash is usually preserved.
  • Stashes live in the local repository and are not automatically synced to the remote.
  • If the changes are stable enough, creating a branch or making a regular commit is usually cleaner than accumulating stashes long-term.
git stash list
git stash show -p stash@{0}
git stash apply stash@{0}
git stash drop stash@{0}