Browse topics

git stash push and apply

Version
2.51
Review
Draft

Summary#

git stash push records local edits and resets those paths to their committed state. git stash apply restores the edits while keeping the stash entry.

Syntax#

sh
git stash push -m "partial draft"
git stash apply 'stash@{0}'
git stash apply --index 'stash@{0}'

Example#

sh
(
  set -eu
  scratch=$(mktemp -d "${TMPDIR:-/tmp}/devlookup-git.XXXXXX")
  trap 'rm -rf -- "$scratch"' EXIT
  unset GIT_CONFIG_GLOBAL $(git rev-parse --local-env-vars)
  export GIT_CONFIG_NOSYSTEM=1 GIT_TEMPLATE_DIR= LC_ALL=C
  export HOME="$scratch" XDG_CONFIG_HOME="$scratch" GIT_ATTR_NOSYSTEM=1
  mkdir "$scratch/work"
  cd "$scratch/work"
  git init -q -b trunk
  git config user.name "Reference Example"
  git config user.email example@example.invalid
  printf 'published\n' > note.txt
  git add note.txt
  git commit -qm "Add note"

  printf 'draft\n' > note.txt
  git stash push -qm "Revise note"
  cat note.txt
  git stash apply -q
  cat note.txt
  git stash list --format='%gd %s'
)

Expected output#

Text
published
draft
stash@{0} On trunk: Revise note

Common mistakes#

Untracked files stay in the working tree unless you request --include-untracked. Use apply --index if you also need the staged state back. Applying edits onto a different base can produce conflicts. Use pop only if you want Git to drop the entry after a successful application.

Version applicability#

Git 2.13 added stash push. Git 1.7.7 introduced --include-untracked.

The complete example requires Git 2.28 or later for git init -b.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0