Auto-detect and restore git stashes when switching branches
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
- User was on
refactor/claudebranch with uncommitted changes - Claude helped create a PR by switching to a new branch (
fix/workflow-yaml-syntax-error) - Git automatically stashed the uncommitted changes during the branch switch
- After PR creation, Claude switched back to
refactor/claude - Claude did not check for or restore the stash
- User continued working, unaware that several hours of work (including a new ViewModeSelector component integration) had been reverted
- User only discovered the regression when testing the application
- 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:
- Check for uncommitted changes: Run
git status --porcelain - 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:
- Check for stashes: Run
git stash list - 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)
```
- Proactively suggest restoration rather than waiting for user to discover missing work
Additional safeguards:
- Status checks after branch operations: Always run
git statusand 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
- Prefer commits over stashes: When switching branches for small fixes/PRs, suggest creating a WIP commit instead of relying on git's auto-stash
- Branch isolation mode: Offer a "safer" branch switching mode that uses worktrees instead of checkout
- 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 changesgit stash list --date=iso- List stashes with timestampsgit stash show -p stash@{0}- Preview stash contentsgit stash apply- Restore without removing from stash listgit 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:
- Anticipate problems before they occur
- Preserve user work above all else
- Guide users through safe git practices
- Recover gracefully when issues arise
This incident represents a gap where Claude's git automation outpaced its safety checks.
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗