Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Staging and Committing

git stash command

Reference for git stash: push, list, show, apply, pop, drop and branch, plus -u, --keep-index, pathspecs and what happens on conflicts.

git stash saves your uncommitted changes on a stack and restores a clean working tree, so you can switch tasks without committing half-finished work. Later you bring the changes back with git stash pop or git stash apply.

A stash is stored as a commit (actually a small group of commits holding the index and working tree state) referenced by refs/stash; older entries live in its reflog as stash@{1}, stash@{2} and so on.

Terminal
git stash push -m "message"   # save tracked changes
git stash list
git stash pop                 # apply the newest and drop it

Try it

Bash Push, list, apply and drop

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Untracked files, keep-index and pathspecs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Turn a stash into a branch

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
git stash [push] [-p] [-u | -a] [-k] [-m <msg>] [-S] [--] [<pathspec>...]
git stash list [<log-options>]
git stash show [-p | --stat] [--include-untracked] [<stash>]
git stash apply [--index] [<stash>]
git stash pop [--index] [<stash>]
git stash drop [<stash>]
git stash clear
git stash branch <branch> [<stash>]
git stash create | store
Name Description
push Save changes (the default subcommand). git stash save is deprecated.
-m, --message Give the stash a description.
-u, --include-untracked Also stash untracked files.
-a, --all Also stash ignored files.
-k, --keep-index Stash everything but leave staged changes in the index and tree.
-p, --patch Choose hunks interactively.
--staged (-S) Stash only what is staged.
<pathspec> Limit the stash to these paths.
apply / pop Reapply a stash; pop also drops it when it applied cleanly.
--index Restore the staged state too, not only the working tree.
branch Check out a new branch at the commit the stash was made on, apply the stash and drop it.
create / store Plumbing: build a stash commit, then add it to the stash reflog.

Returns: push prints "Saved working directory and index state ..." and "No local changes to save" when there is nothing to stash (exit status 0).

Errors: a conflicting apply or pop stops with the usual conflict markers and keeps the stash; unmerged paths block push.

Usage notes

  • Untracked files are skipped unless you pass -u. This surprises people who then git clean and lose files.
  • pop keeps the stash on conflict. Resolve the conflict, then run git stash drop yourself.
  • Stashes do not belong to branches. stash@{0} can be applied anywhere, but a stash made on one commit may conflict on another; git stash branch avoids that by recreating the original base.
  • Index state is lost by default. apply restores staged files as unstaged unless you pass --index.
  • Stashes are local. They are not pushed or fetched and can disappear with git stash clear. A dropped stash commit can still be found with git fsck --unreachable | grep commit until garbage collection.
  • Use -m so git stash list explains each entry.
  • Autostash helps pull and rebase: git pull --autostash and rebase.autoStash stash and reapply automatically.
  • git stash show prints a diffstat; add -p for the full diff.