git stash

stash apply versus pop, --index, untracked files, and conflict handling, checked on Git 2.39.5.

コマンドページの本文は現在英語です。

git stash records dirty state and, by default, resets the tracked files to HEAD so you can work on something else. git stash push -m records a message. apply puts the stash back and keeps the stash entry. pop applies and then drops the entry, but only if the apply succeeds. If the apply conflicts, Git keeps the stash.

Examples were checked on Git 2.39.5 in isolated temporary repositories. They use a fixed practice identity. Do not run stash, pop, or drop in a repository until you have reviewed git status; a pop that conflicts still changes the working tree.

Examples were checked against Git 2.39.5. See the official documentation for the complete command.

Git command catalog → · Git tutorial overview →

apply keeps the stash, pop drops it

After git stash push, the tracked file in the example returns to a clean status. apply -q restores the edit as an unstaged change and leaves stash@{0} in the list. A later pop -q on a clean worktree applies the same edit again and, on success, removes the stash entry.

Without --index, a stash that held both staged and unstaged edits is applied as working-tree changes only. The next section restores the index as well.

git stash push, git stash list, git stash apply, git stash pop

git stash push [-q] [-m <message>]
git stash list
git stash apply [-q] [<stash>]
git stash pop [-q] [<stash>]

Save dirty tracked files, reapply them, and drop the entry only after a successful pop.

ParameterDescription
-m <message>A description stored with the stash. The list line then reads On <branch>: <message>.
-q / --quietReduce progress output. apply and pop still print conflicts when they occur.

Returns: push records a stash and cleans tracked dirty files. apply restores them and keeps the list entry. pop restores them and drops the entry when the apply succeeds.

  • The example calls restore --worktree after apply only in this temporary repository, so the following pop can run on a clean file. That restore discards the applied worktree edit.
bash
git init -b main >/dev/null
git config user.name "Practice Author"
git config user.email "[email protected]"
git config commit.gpgsign false
git config color.ui false
mkdir -p .disabled-hooks
git config core.hooksPath ".disabled-hooks"
printf 'base\n' > f.txt
git add f.txt
git commit -m "Base" >/dev/null
printf 'edit\n' > f.txt
git stash push -q -m "sample stash"
echo "after-push"
st=$(git status --porcelain=v1)
if [ -z "$st" ]; then echo "(clean)"; else printf "%s\n" "$st"; fi
git stash list
git stash apply -q
echo "after-apply"
git status --porcelain=v1
git stash list
git restore --worktree -- f.txt
git stash pop -q
echo "after-pop"
git status --porcelain=v1
st=$(git stash list)
if [ -z "$st" ]; then echo "(empty-list)"; else printf "%s\n" "$st"; fi

Expected output

after-push
(clean)
stash@{0}: On main: sample stash
after-apply
 M f.txt
stash@{0}: On main: sample stash
after-pop
 M f.txt
(empty-list)

Related: git status, git restore

Official documentation →

--index, untracked files, and conflicts

git stash push -u, also --include-untracked, records untracked files and removes them from the worktree. Ignored files are still left alone unless you use --all. apply --index restores the staged and unstaged state that was recorded, so a pre-stash MM file comes back as MM.

If the worktree has moved on, pop can conflict. In the example, pop exits 1, prints that the stash entry is kept, and leaves UU on the path. The list still shows stash@{0}. Resolve or abort the conflict before you drop anything; a failed pop must not be retried as a second merge until the file is usable again.

git stash push -u, git stash apply --index, git stash pop

git stash push -u [-m <message>]
git stash apply --index [<stash>]
git stash pop [--index] [<stash>]

Stash untracked files, restore the index, and keep the stash when pop conflicts.

ParameterDescription
-u / --include-untrackedAlso record and remove untracked files. Ignored files stay unless --all is used.
--indexTry to restore the index as well as the working tree. Without it, staged and unstaged edits often come back as a single worktree change.

Returns: On a clean apply, tracked and untracked files match the recorded stash. On a conflict, pop exits 1 and keeps the stash entry.

Exit status

  • 1 — pop reports a conflict, keeps the stash, and leaves the path unmerged (UU in porcelain). Measured on Git 2.39.5.
  • The conflict text is from Git. The later "Other edit" commit uses a later fixed timestamp so its hash stays stable in this isolated script.
bash
git init -b main >/dev/null
git config user.name "Practice Author"
git config user.email "[email protected]"
git config commit.gpgsign false
git config color.ui false
mkdir -p .disabled-hooks
git config core.hooksPath ".disabled-hooks"
printf 'base\n' > f.txt
git add f.txt
git commit -m "Base" >/dev/null
printf 'staged-edit\n' > f.txt
git add f.txt
printf 'worktree-edit\n' > f.txt
printf 'loose\n' > u.txt
echo "before"
git status --porcelain=v1
git stash push -q -u -m "index and untracked"
echo "after-push-u"
st=$(git status --porcelain=v1)
if [ -z "$st" ]; then echo "(clean)"; else printf "%s\n" "$st"; fi
git stash apply -q --index
echo "after-apply-index"
git status --porcelain=v1
# reset this isolated repo to a conflicting commit for pop
git restore --staged --worktree -- f.txt
rm -f u.txt
git stash drop -q
printf 'stashed\n' > f.txt
git stash push -q -m "will conflict"
printf 'other\n' > f.txt
git add f.txt
GIT_AUTHOR_DATE='2024-01-15T16:00:00+0000' GIT_COMMITTER_DATE='2024-01-15T16:00:00+0000' git commit -m "Other edit" >/dev/null
git stash pop -q
echo "pop-conflict-exit=$?"
echo "list-after-conflict"
git stash list
echo "porcelain-after-conflict"
git status --porcelain=v1

Expected output

before
MM f.txt
?? u.txt
after-push-u
(clean)
after-apply-index
MM f.txt
?? u.txt
Auto-merging f.txt
CONFLICT (content): Merge conflict in f.txt
The stash entry is kept in case you need it again.
pop-conflict-exit=1
list-after-conflict
stash@{0}: On main: will conflict
porcelain-after-conflict
UU f.txt

Official documentation →