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 [push | list | show | pop | drop | apply | branch | save | clear | create | store]

PARAMETERS

push [-u | --include-untracked] [-a | --all] [-p | --patch] [-m | --message=] [-- [pathspec...]]
    Saves your local modifications to a new stash entry.

list
    Lists all stash entries.

show [-p | --patch] []
    Shows the changes recorded in the stash. defaults to the most recent stash.

pop []
    Removes a single stash entry from the list and applies it to your working copy. defaults to the most recent stash.

drop []
    Removes a single stash entry from the list. defaults to the most recent stash.

apply []
    Applies the changes recorded in a stash to your working copy. Does not remove the stash. defaults to the most recent stash.

branch []
    Creates and checks out a new branch starting from the commit at which the was originally created, then applies the changes recorded in to the new working copy. defaults to the most recent stash.

save [-u | --include-untracked] [-a | --all] [-p | --patch] []
    Deprecated. Saves your local modifications to a new stash entry. Use git stash push instead.

clear
    Removes all stash entries.

create
    Creates a stash entry (which is a regular commit object) and returns its object name, without storing it anywhere in the ref namespace.

store
    Stores a given stash entry (which is a dangling commit object) in the ref namespace.

DESCRIPTION

The git stash command allows you to save changes you've made to your working directory and staging area without committing them. This is useful when you need to switch branches or work on something else and don't want to commit unfinished work. It essentially takes the dirty state of your working directory (tracked, but modified files, as well as staged changes) and saves it on a stack of unfinished changes that you can reapply at any time. Think of it like a temporary holding area for your in-progress modifications. When you're ready to resume your work, you can pop the stashed changes back into your working directory. This makes it easy to context-switch between different tasks without losing your work.

CAVEATS

Stashing only works on tracked files by default. You need to use the -u or -a options to include untracked files. Also, conflicts can occur when applying a stash if the base of the stash is different from the current HEAD of the branch.

USAGE EXAMPLES

Stash your current changes:
git stash push -m "My Changes"

List your stashes:
git stash list

Apply the most recent stash:
git stash pop

Apply a specific stash:
git stash apply stash@{2}

Create a branch from a stash:
git stash branch mynewbranch stash@{0}

Clear all stashes:
git stash clear

SEE ALSO

Copied to clipboard