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.
git stash push -m "message" # save tracked changes git stash list git stash pop # apply the newest and drop it
Try it
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.
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.
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
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 thengit cleanand lose files. popkeeps the stash on conflict. Resolve the conflict, then rungit stash dropyourself.- 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 branchavoids that by recreating the original base. - Index state is lost by default.
applyrestores 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 withgit fsck --unreachable | grep commituntil garbage collection. - Use
-msogit stash listexplains each entry. - Autostash helps pull and rebase:
git pull --autostashandrebase.autoStashstash and reapply automatically. git stash showprints a diffstat; add-pfor the full diff.
Related
git restore,git clean,git reset,git worktree- Lessons: Stash, Staging