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]] [optional_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 [] [] []

Common forms:
git stash push [-p|--patch] [-k|--[no-]keep-index] [-u|--include-untracked] [-a|--all] [-q|--quiet] [-m|--message <message>] [--] [<pathspec>...]
git stash pop [--index] [<stash>]
git stash apply [--index] [<stash>]
git stash list [<options>]
git stash show [<options>] [<stash>]
git stash drop [-q|--quiet] [<stash>]
git stash clear
git stash branch <branchname> [<stash>]

PARAMETERS

push
    Saves your local modifications to a new stash entry. This is the default action when no subcommand is given, e.g., git stash -m 'message'.

list
    Lists the stash entries that you currently have, showing their index and message.

show [<stash>]
    Shows the changes recorded in the given stash entry (defaults to the latest one, stash@{0}).

pop [--index] [<stash>]
    Removes a single stash entry from the stash list and applies it on top of the current working tree. Use --index to try to restore the index state.

apply [--index] [<stash>]
    Applies a single stash entry from the list onto the current working tree, but leaves it in the stash list for potential reuse. Use --index to try to restore the index state.

drop [<stash>]
    Removes a single stash entry from the stash list (defaults to stash@{0}).

clear
    Removes all stash entries from the stash list.

branch <branchname> [<stash>]
    Creates and checks out a new branch named <branchname> starting from the commit at which the <stash> was originally created, and applies the changes from <stash> to it.

-p, --patch
    Interactively select hunks from the diff to be stashed. Similar to git add -p.

-k, --keep-index
    Don't stash anything that has already been added to the index. Only stash unstaged changes.

-u, --include-untracked
    Includes untracked files in the stash. These files are typically not version controlled.

-a, --all
    Includes untracked and ignored files in the stash, in addition to tracked files.

-m <message>, --message <message>
    Associates a message with the new stash entry, making it easier to identify later.

DESCRIPTION

git stash is a powerful command that temporarily shelves (or stashes) changes you've made to your working directory and puts them on a stack of unfinished changes. This is incredibly useful when you need to switch branches quickly without committing your current work, or when you want to pull new changes without conflicts from your work-in-progress. It cleans your working directory, making it ready for other operations, then allows you to re-apply the stashed changes later. By default, it saves both staged and unstaged changes. You can optionally include untracked and even ignored files.

CAVEATS

  • By default, git stash does not stash new, untracked files. Use -u or -a to include them.
  • Applying a stash might result in conflicts if your current working directory has changes that overlap with the stashed changes. These conflicts must be resolved manually.
  • git stash pop removes the stash from the stack after applying, while git stash apply leaves it there. Choose based on whether you intend to reuse the stash.
  • Binary files can be stashed, but git stash show typically won't display their differences in a human-readable format.

<I>STASH REFLOG</I>

git stash maintains its own reflog, which is a history of where the stash pointer has been. This means that even if you accidentally `drop` a stash, you can often recover it by inspecting the `reflog` of `refs/stash` (e.g., `git reflog stash`) and then using `git stash apply` `stash@{}` or `git checkout` `stash@{}`.

<I>WORKING WITH MULTIPLE STASHES</I>

While git stash operations (like `pop` or `apply`) default to the most recent stash entry (stash@{0}), you can specify any stash entry by its index (e.g., stash@{1}, stash@{2}) obtained from git stash list. This allows you to manage and apply older or specific stashes as needed, offering great flexibility when juggling multiple unfinished tasks.

HISTORY

The git stash command was introduced relatively early in Git's history (around Git 1.5.3) to address the common workflow problem of needing to switch contexts or pull changes without committing unfinished work. Initially, it primarily used the save subcommand (now deprecated in favor of push). Over time, it has evolved with new options like --include-untracked and --all to provide more flexibility in what gets stashed. It has become an indispensable tool for many Git users, allowing for agile context switching and efficient management of work in progress, embodying Git's flexibility.

SEE ALSO

git status(1), git diff(1), git reset(1), git commit(1), git clean(1)

Copied to clipboard