Skip to content

Drill: Advanced Stash Usage

Goal

Use git stash to save work-in-progress changes with messages, stash specific files, and understand pop vs apply.

Setup

  • A git repository with uncommitted changes

Commands

Set up a practice scenario:

mkdir /tmp/stash-drill && cd /tmp/stash-drill && git init
echo "base" > file1.txt && echo "base" > file2.txt && git add . && git commit -m "initial"
echo "wip change 1" > file1.txt
echo "wip change 2" > file2.txt

Stash with a descriptive message:

git stash push -m "WIP: refactoring auth module"

Stash only specific files:

echo "change1" > file1.txt && echo "change2" > file2.txt
git stash push -m "only file1 changes" -- file1.txt

Stash including untracked files:

echo "new" > newfile.txt
git stash push -u -m "including untracked files"

List all stashes:

git stash list

Show what a stash contains:

git stash show -p stash@{0}

Apply a stash (keeps it in the stash list):

git stash apply stash@{0}

Pop a stash (applies and removes from list):

git stash pop stash@{0}

Drop a specific stash:

git stash drop stash@{0}

Create a branch from a stash:

git stash branch new-feature-branch stash@{0}

Clear all stashes:

git stash clear

What to Look For

  • apply keeps the stash entry; pop removes it on successful apply
  • Stash messages make it easy to identify stashes in a long list
  • stash push -- <file> lets you stash only part of your working directory
  • stash branch is useful when the stash conflicts with current HEAD

Common Mistakes

  • Using pop instead of apply and losing the stash reference if conflicts occur
  • Forgetting -u to include untracked files in the stash
  • Not giving stashes descriptive messages and losing track of what each contains
  • Using git stash clear accidentally and losing all stashed work

Cleanup

rm -rf /tmp/stash-drill