Git Stash



git stash command is used in saving the work without committing it by temporarily storing changes in the working directory that aren't ready to be committed.

This helps us to transition between activities or put off unfinished modifications for the moment without stacking up commit history.

It lets us undo all of the changes we've done since the last commit from the index and the working directory, restoring our working directory to its original state.

Salient Features

Saving Work temporarily - Both staged and unstaged changes can be saved temporarily.

Multiple Stashes - Multiple sets of changes can be saved and reapplied in any order.

Stash List - A list of all the stashed changes can be seen at a go.

Applying Stash - The stashed changes can be applied later using apply and pop options. git stash pop deletes the stash after it is being applied.

Stash Specific Changes - Only a selective part of the changes can be stashed.

Stashes are created with the default label of "WIP on branchname...", but we can add a more informative statement.

  • Older stashes can be retrieved using the reflog notation (stash@{1}, stash@{2.hours.ago}, etc.), with the most recent stash kept in refs/stash.

  • Additionally, stashes can be accessed via index stash@{n}, where {n} is the stash number.

Commands

The generic command to stash the changes is git stash.

git stash

push

The command git stash push deletes the most recent modifications from the index and working directory and stores them in a new stash.

  • We have the option to add a detailed <message> to the stash to indicate what's inside.

  • Removing push limits non-option arguments to avoid accidental stashing but permits a fast snapshot.

  • The command git stash -p allows interactive selection of modifications to stash; it is a shortcut for git stash push -p.

  • Pathspec components can be placed after -- to make it clearer which files or modifications to stash.

save

The command git stash save is deprecated and should be replaced with git stash push.

git stash save
  • Specifying individual files (pathspec) is not supported by git stash save, in contrast to git stash push.

  • Any further inputs passed to git stash save are concatenated to produce the stash message, rather than pathspec.

list

All presently saved stash entries are shown by running git stash list.

git stash list
  • Each stash entry is named stash@{n} in a sequential manner, with stash@{0} denoting the most recent stash.

  • It displays the name of the branch that was in use at the time each stash was made.

  • Every stash entry has a brief synopsis of the commit that served as its basis.

show

Changes that are documented in a stash entry are shown by the git stash show command.

git stash show
  • It displays a summary of changes by default (diffstat), but with <diff-options>, it may show the actual changes in any format that is supported by git diff.

  • Configuration variables that control default behaviors for statistics and patch display include stash.showStat and stash.showPatch.

  • When displaying stashed updates, the stash.showIncludeUntracked setting controls whether --include-untracked is enabled by default.

apply

Changes from a stash are applied to the current working tree and index (staging area) with the command git stash apply.

git stash apply
  • It doesn't remove the stash from the stash list like git stash pop does.

  • We can use any commit that resembles a stash generated by git stash push or git stash create, or we can specify a specific stash entry by providing its name (stash@{n}).

  • To operate more quietly and reduce output verbosity, use -q or --quiet.

branch

Creates a new branch <branchname> starting from the commit where <stash> was originally created.

git stash branch
  • Updates the working tree and new branch's index with the stored modifications.

  • When <stash> is entered in the format stash@{<revision>}, the stash is deleted once it has been applied.

  • Good for quickly branching with changes stashed, especially when a simple git stash apply becomes complicated due to conflicts.

clear

All entries in the stash are eliminated using the git stash clear command.

git stash clear
  • These entries are prone to clipping after they are removed and might not be recovered.

drop

One stash record is eliminated from the list of stash entries using the git stash drop command.

git stash drop
  • To suppress confirmation messages, use it in combination with the -q or --quiet option.

create

The command git stash create generates a typical commit object called a stash entry and returns its object name without saving it in the ref namespace.

git stash create
  • Compared to git stash push, this capability is less frequently used and is mainly meant for scripting reasons.

store

The git stash store command modifies the stash reflog by storing a stash that was created with git stash create, a dangling merge commit, into the stash ref.

git stash store
  • The command git stash push is normally preferable for generating and storing stashes.

  • This command is mostly meant for scripting reasons and is not used directly.

Options

The git stash command supports the following options:

-a

--all

  • When used in combination with git stash push and git stash save, the -a or --all option stashes all changes, including staged, modified, and untracked files including those that are often ignored.

  • It also uses git clean to clean up untracked files after stashing.

-u

--include-untracked

--no-include-untracked

  • -u / --include-untracked - Stashes all changed, staged, and untracked files including those that are often ignored when used in combination with git stash push or git stash save.

    The git clean is then used to clean them up.

    Shows untracked files in the stash entry's diff when used with git stash show.

  • --no-include-untracked - The opposite of -u, this command prevents untracked files from being displayed or stashed in diffs when using the commands mentioned above.

--only-untracked

  • Shows only the untracked files from the stash entry as part of the diff; exclusive to the git stash show command.

--index

  • When used with the git stash pop and git stash apply commands, it tries to reapply modifications to the index (staging area) as well as the working tree.

-k

--keep-index

--no-keep-index

  • When combined with git stash push or git stash save, the option -k / --keep-index :- Stashes only unstaged updates while preserving all changes that have already been put to the index (staging area).

  • In contrast to -k, --no-keep-index stashes all modifications, including those that are already in the index, when used in combination with the above commands.

git stash temporarily stashes all changes in the working directory. The stashed changes can be reapplied when needed.

Advertisements