LinuxCommandLibrary

git-stash

Temporarily save changes to a clean working directory

TLDR

Stash current changes with a message, except new (untracked) files

$ git stash push [[-m|--message]] [stash_message]
copy

Stash current changes, including new untracked files
$ git stash [[-u|--include-untracked]]
copy

Interactively select parts of changed files for stashing
$ git stash [[-p|--patch]]
copy

List all stashes (shows stash name, related branch and message)
$ git stash list
copy

Show the changes as a patch between the stash (default is stash@{0}) and the commit back when stash entry was first created
$ git stash show [[-p|--patch]] [stash@{0]}
copy

Apply a stash (default is the latest, named stash@{0})
$ git stash apply [optional_stash_name_or_commit]
copy

Drop or apply a stash (default is stash@{0}) and remove it from the stash list if applying doesn't cause conflicts
$ git stash pop [optional_stash_name]
copy

Drop all stashes
$ git stash clear
copy

SYNOPSIS

git stash [push] [options] [--] [<pathspec>]
git stash { list [<options>] [<pattern>] | show [<options>] [<stash>] | drop [<options>] <stash>... | pop [<options>] [<stash>] | apply [<options>] [<stash>] | branch <branchname> [<stash>] | clear | empty | create | store [<options>] <commit> }

PARAMETERS

-p, --patch
    interactively select hunks to stash (push, list, show)

-k, --keep-index
    keep staged files in index (push)

--staged
    stash only staged changes (push, Git 2.35+)

-u, --include-untracked
    stash untracked files too (push, pop, apply)

-a, --all
    stash untracked and ignored files (push, pop, apply)

-q, --quiet
    suppress messages (push, drop, store)

-m, --message <message>
    override stash message (push, store)

--grep=<pattern>
    list stashes matching pattern (list)

--stat
    show diffstat (show)

--patch
    show patch (show, list)

--index
    apply working directory and index (pop, apply)

--help
    display help

DESCRIPTION

git stash is a Git subcommand that temporarily saves (stashes) uncommitted changes in the working directory and staging area, reverting them to match the last commit. This enables clean switches between branches or tasks without committing incomplete work.

It creates a hidden commit stored in refs/stash, allowing multiple stashes indexed as stash@{0} (latest), stash@{1}, etc. By default, it stashes tracked files' changes (staged and unstaged) but ignores untracked files.

Key uses: quickly fix urgent bugs on other branches, experiment safely, or collaborate by sharing stashes. Subcommands manage the stash stack: push (default) to save, list to view, pop/apply to restore, drop/clear to remove, show to inspect, branch to branch from a stash, and create/store for advanced scripting.

Options customize behavior, like including untracked files (-u) or keeping the index (-k). Restoring may cause merge conflicts, resolved manually like any merge.

CAVEATS

Does not stash untracked/ignored files by default.
Restoring (pop/apply) may cause conflicts needing manual fix.
Stash@{n>0} can be lost if refs/stash corrupted or garbage collected.
save deprecated since Git 2.13; use push.

STASH REFERENCES

Stashes stored as stash@{0} (top), stash@{n}. Use git stash list to view; git stash show -p stash@{1} previews.

COMMON WORKFLOW

git stash push -m 'WIP'
git checkout other-branch
git stash pop
Or git stash branch newfeat stash@{0} to recover.

HISTORY

Introduced in Git 1.6.3 (June 2008) by Shawn O. Pearce. Evolved with subcommands: push added in 2.13 (2017) replacing deprecated save; --staged in 2.35 (2022). Widely used for workflow flexibility in modern Git (2.40+).

SEE ALSO

Copied to clipboard