Git Cheatsheet

Stashing

Use this Git reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

Basic Stashing

git stash                       # Stash tracked modified files + staged changes
git stash push                  # Explicit form of git stash (same as above)
git stash push -m "message"     # Stash with a descriptive message
git stash push <file>           # Stash only a specific file
git stash push -- <file1> <file2>  # Stash specific files
git stash push -p               # Interactively choose hunks to stash (patch mode)

Stash Options

git stash -u                    # Also stash untracked files (--include-untracked)
git stash -a                    # Also stash untracked AND ignored files (--all)
git stash --keep-index          # Stash but leave staged changes in the index
git stash --no-keep-index       # Also stash staged changes (default)

Listing Stashes

git stash list                  # List all stashes (newest first)
git stash list --stat           # Include file summary for each stash

Stash naming: stashes are referenced as stash@{0} (most recent), stash@{1}, etc.

Inspecting a Stash

git stash show                  # Summary of files in the most recent stash
git stash show stash@{1}        # Summary of a specific stash
git stash show -p               # Full diff of most recent stash
git stash show -p stash@{1}     # Full diff of a specific stash
git stash show --stat           # File statistics for the most recent stash

Applying and Popping Stashes

git stash apply                 # Apply most recent stash (keep it in the stash list)
git stash apply stash@{2}       # Apply a specific stash
git stash apply --index         # Also restore the staged state (not just working tree)

git stash pop                   # Apply most recent stash AND remove it from the list
git stash pop stash@{1}         # Pop a specific stash
git stash pop --index           # Restore staged state as well

apply vs pop: Use apply when you want to apply the same stash to multiple branches. Use pop for one-time application.

Deleting Stashes

git stash drop                  # Delete the most recent stash
git stash drop stash@{1}        # Delete a specific stash
git stash clear                 # Delete ALL stashes (irreversible)

Creating a Branch from a Stash

Useful when a stash no longer applies cleanly to the current branch:

git stash branch <branch-name>              # Create branch from most recent stash
git stash branch <branch-name> stash@{1}   # Create branch from a specific stash

This creates the branch at the commit where the stash was created, applies the stash, and drops it if successful.

Resolving Stash Conflicts

If git stash pop or git stash apply has conflicts:

git stash pop                   # Conflicts appear in working tree with markers
# Fix conflict markers in the files
git add <resolved-file>
# Do NOT run git stash drop automatically — pop leaves the stash on conflict
git stash drop                  # Manually drop after resolving

Common Workflows

Switch branches mid-work

git stash push -m "WIP: login form validation"
git switch hotfix/urgent-bug
# ... fix the bug, commit, push ...
git switch feature/login
git stash pop

Stash only unstaged changes (keep staged)

git stash push --keep-index -m "just unstaged"

Move changes to a new branch

git stash
git switch -c feature/new-branch
git stash pop

Apply stash to a different branch

git stash push -m "wip changes"
git switch other-branch
git stash apply stash@{0}

Stash Internals

Each stash creates two (or three) commits in .git/refs/stash: - stash@{0}^1 — the working tree changes - stash@{0}^2 — the index (staged) changes - stash@{0}^3 — untracked files (if -u was used)

git log --oneline stash@{0}^1  # Inspect stash internals
git diff stash@{0}..HEAD        # Diff between stash and current HEAD

Gotchas

git stash does NOT stash untracked files by default. Use -u to include them.

git stash clear is permanent. Stashes are not in the reflog, so they cannot be recovered after clearing.

After git stash pop with a conflict, the stash is NOT automatically removed — you must resolve and then run git stash drop manually.

Stash indices (stash@{0}, stash@{1}) shift when you pop/drop — always check git stash list before operating on a numbered stash.