Auto-detect and restore git stashes when switching branches

Resolved 💬 2 comments Opened Jan 22, 2026 by punkhat6317 Closed Feb 27, 2026

Problem

When Claude Code assists with switching git branches, it doesn't check for or restore stashed changes when returning to the original branch. This can lead to significant work loss that's difficult to detect until the user tests their code.

What happened

  1. User was on refactor/claude branch with uncommitted changes
  2. Claude helped create a PR by switching to a new branch (fix/workflow-yaml-syntax-error)
  3. Git automatically stashed the uncommitted changes during the branch switch
  4. After PR creation, Claude switched back to refactor/claude
  5. Claude did not check for or restore the stash
  6. User continued working, unaware that several hours of work (including a new ViewModeSelector component integration) had been reverted
  7. User only discovered the regression when testing the application
  8. Had to manually investigate, find the stash, and restore the work

Impact

  • Hours of work lost (temporarily)
  • User frustration from discovering regressions after the fact
  • Trust erosion in Claude Code's git workflow management
  • Manual recovery required (checking stash history, restoring files)

Proposed Solution

When Claude Code assists with branch switching, it should:

Before switching branches:

  1. Check for uncommitted changes: Run git status --porcelain
  2. Warn the user explicitly:

```
⚠️ Warning: You have uncommitted changes. Git will automatically stash these when switching branches.

I can:
A) Commit these changes first (recommended)
B) Stash them manually so we can restore later
C) Proceed with branch switch (git will auto-stash)

Which would you prefer?
```

After returning to original branch:

  1. Check for stashes: Run git stash list
  2. If stashes exist from recent branch operations:

```
ℹ️ I found a git stash from when we switched branches earlier.

Stash details:

  • Created: 2026-01-22 at 3:52 PM
  • Files: App.tsx, Legend.tsx, BlipLayer.tsx, GridLayer.tsx, FilterControls.tsx, useForceSimulation.ts

Would you like me to restore these changes? (git stash apply)
```

  1. Proactively suggest restoration rather than waiting for user to discover missing work

Additional safeguards:

  • Status checks after branch operations: Always run git status and report any differences from expected state
  • Stash awareness: Track when stashes are created during Claude-assisted operations
  • Recovery guidance: If work loss is detected, provide clear instructions for recovery

Alternative/Complementary Approaches

  1. Prefer commits over stashes: When switching branches for small fixes/PRs, suggest creating a WIP commit instead of relying on git's auto-stash
  2. Branch isolation mode: Offer a "safer" branch switching mode that uses worktrees instead of checkout
  3. State persistence: Keep track of branch state (uncommitted changes, stashes) across Claude sessions

Example User Flow (Proposed)

User: "Create a PR to fix this YAML error"

Claude: ⚠️  Warning: You have uncommitted changes in 6 files.
        Before creating a PR on a new branch, would you like to:

        A) Commit these changes first (recommended)
        B) Create a WIP commit we can amend later
        C) Stash them (I'll remind you to restore when we return)

User: "C"

Claude: Creating stash... ✓
        [Creates PR on new branch]
        [Returns to refactor/claude]

        ℹ️  Restoring your stashed changes from earlier...
        Applied stash: WIP on refactor/claude: 088d99e

        Your work is now restored. You can continue where you left off.

Technical Implementation Notes

Key git commands needed:

  • git status --porcelain - Check for uncommitted changes
  • git stash list --date=iso - List stashes with timestamps
  • git stash show -p stash@{0} - Preview stash contents
  • git stash apply - Restore without removing from stash list
  • git rev-parse --abbrev-ref HEAD - Track current branch

Why This Matters

Claude Code is helping users with increasingly complex git workflows. As an AI assistant, it should:

  1. Anticipate problems before they occur
  2. Preserve user work above all else
  3. Guide users through safe git practices
  4. Recover gracefully when issues arise

This incident represents a gap where Claude's git automation outpaced its safety checks.

View original on GitHub ↗

This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗