Chapter 12Lesson 04~105 minutes

Stash, Worktrees, Temporary Work, and Parallel Development Contexts: Diagnostics, Failure Modes, Security, and Performance

Diagnose stash/worktree failures including omitted untracked files, conflicted pop, duplicate branch checkout, stale metadata after manual deletion, long-term stash misuse, and wrong-worktree operational execution.

DiagnosticsStash conflictStale metadataContext safety

Learning objectives

  • Apply a preserve-evidence-first diagnostic sequence to hidden local contexts.
  • Explain and repair default stash omission of untracked content.
  • Recover safely from pop conflicts while retaining the stash entry until verified.
  • Diagnose branch checkout exclusivity and stale worktree metadata without bypassing safeguards.
  • Add path/branch/OID assertions to operational worktree preflight.

1. Diagnostic sequence for context-switching failures

  1. Preserve evidence: current path, branch/OID, status, stash list/OIDs, and worktree list.
  2. Inspect status, refs, history, and config.
  3. Identify the affected layer: current worktree/index, stash ref/reflog, shared branch ref, linked-worktree metadata, or external tool context.
  4. Choose the least destructive correction.
  5. Verify that no work remains hidden before dropping stashes or pruning/removing worktree state.
pwd
git rev-parse --show-toplevel
git status --short --branch
git rev-parse HEAD
git stash list
git worktree list --porcelain
git branch -vv

2. Failure mode — “I stashed everything,” but the untracked file is still here

Create one tracked modification and one untracked file, then run a default stash:

git status --short
git stash push -m "default stash demonstration"
git status --short

If the untracked path remains, Git is behaving correctly: default stash scope does not include it. Repair by deciding what that file is:

  • If it is important temporary work, use git stash push -u -m "..." after restoring/applying the tracked context as needed.
  • If it belongs in durable history, stage/commit it on the appropriate branch.
  • If it is disposable output, inspect and remove it deliberately—do not assume stash is a cleanup command.

3. Intentionally broken example — pop conflicts but the stash survives

A branch changed the same line after the stash was created:

git stash pop stash@{0}

Typical output contains lines conceptually like:

Auto-merging service.conf
CONFLICT (content): Merge conflict in service.conf
...
The stash entry is kept in case you need it again.

Interpretation: Git attempted a three-way application; the file cannot be combined automatically; the index now contains conflict stages; and because application failed, the stash ref/reflog entry remains. Inspect:

git status
git diff
git stash list
git stash show -p stash@{0}

Resolve and stage deliberately, then verify the result. Only when the recovered content is safe should you run git stash drop stash@{0}. If current history has diverged substantially, abort the conflicted path state and consider git stash branch from the stash's original base.

4. Intentionally broken example — branch already active in another worktree

git worktree add ../second feature/metrics

Git refuses when feature/metrics is already checked out elsewhere. The failure protects the branch ref. Diagnose:

git worktree list --porcelain
git branch -vv
git -C ../feature-wt status --short --branch

The least destructive repair is usually to use the existing feature worktree, switch that worktree away after making it safe, or remove the worktree when finished. Do not reach for force just to suppress a useful ownership warning.

5. Failure mode — deleting the worktree directory manually leaves administrative metadata

In a disposable lab, suppose ../review-wt was deleted with the file manager instead of git worktree remove. The common repository can still remember the missing path.

git worktree list --porcelain
git worktree prune --dry-run --expire=now

If the directory is genuinely gone and no recovery is needed, prune the stale metadata:

git worktree prune -v --expire=now
git worktree list --porcelain

If the directory was moved and still exists, do not prune first; reconnect it with git worktree repair.

6. Failure mode — stash becomes a months-old “backup”

A stash is local state and can be dropped/cleared; once unreachable it is subject to normal object-retention/pruning behavior. Recovery after accidental drop is best-effort, not a service-level guarantee. If git stash list looks like a backlog, convert valuable work into branches/commits and delete only entries that have been verified unnecessary.

git stash clear removes every normal stash entry from the list. Do not use it as routine housekeeping until every entry has been inspected or promoted to durable history.

7. Failure mode — running the right tool in the wrong worktree

During a hotfix, two terminals can look almost identical while pointing at different branches/OIDs. Before any deployment, migration, version bump, signing, or release command:

pwd
git rev-parse --show-toplevel
git branch --show-current
git rev-parse HEAD
git status --short --branch
git worktree list

A production script can make these preconditions executable: require the expected absolute repository path, branch policy, clean status, and exact candidate OID before proceeding. The risk is operational context, not only Git syntax.

8. Security where worktree sharing matters

  • Linked worktrees share the object database. Historical secret-bearing objects are visible from all of them; worktrees are not security sandboxes.
  • Repository config is shared by default, so do not store privileged tokens there to “make only the hotfix worktree work.”
  • Untracked credentials in one worktree are not automatically copied to another, but tools can still read external home-directory credentials unless isolated by the runtime.
  • For untrusted CI code, prefer a stronger isolated workspace rather than relying on worktrees as a security boundary.

9. Performance tradeoffs that actually follow from the model

Worktrees save object-download and object-storage duplication, which can be valuable for large repositories and local parallel testing. They still duplicate checked-out files and often duplicate build outputs. Concurrent operations also share repository refs/object maintenance. Use worktrees for controlled parallelism, not as an unlimited job sandbox.

10. Red-zone commands and actions

Preserve evidence before: git stash drop/clear, forcing removal of a dirty worktree, pruning metadata for a path you have not verified missing, manually deleting worktree directories, broad git clean, hard reset, force-updating refs, reflog expiry, or object pruning. Each can reduce recovery options.

11. Symptom → evidence → narrow correction

Symptom Evidence First safe response
Untracked file remains after stash status + stash scope Decide -u, commit, or deliberate deletion
Pop conflict status/diff/stash list Resolve or recover via original stash base; keep stash until safe
Branch “already checked out” worktree list + branch -vv Use the owning worktree
Missing directory still listed worktree list + prune dry-run Repair if moved; prune if truly gone
Tool ran against wrong source path + branch + exact OID + logs Stop, preserve artifacts/evidence, rerun only after context validation

12. Knowledge check

Question 1. Why can a default stash leave a dirty-looking repository?

Question 2. What is the most important fact after a conflicted stash pop?

Question 3. A worktree path disappeared because a USB drive is temporarily disconnected. Prune or lock?

Question 4. Why is a branch-already-active error useful?

Question 5. What should an incident script verify before acting in a multi-worktree repository?

13. Summary

Hidden-state failures are diagnosable when you inspect both dimensions: stash entries and worktree registrations. Keep the stash after conflicts, respect branch ownership, repair versus prune based on whether a directory still exists, and make operational tools assert the intended worktree/OID before acting.

Next

Checkpoint an urgent hotfix without losing unfinished feature work

Lesson 5 applies the decision rule, creates a hotfix worktree, integrates the fix, restores feature state, and proves that no stash or worktree metadata remains hidden.

Authoritative references

 git-stash
 git-worktree
 git-fsck

Keep the academy open

Support free, practical DevOps education.

Every lesson is designed to remain readable in a browser, downloadable from GitHub, and usable without a paid learning platform. Contributions help expand and maintain the curriculum.

Ethereum / ERC-20
0x716c4Ab160C4B66F31a28AE2448BfF68fc3a2ef0 Send only Ethereum/ERC-20 compatible assets to this address.