Git Based Undo/Checkpoint in Claude Code

4 months ago 29

Git Branch-Based Claude Code Session Management

  • Session Start: Automatically create a new feature branch from current HEAD:
    git checkout -b claude-session-[timestamp]-[topic]
Example: `claude-session-20241216-143022-refactor-auth` - **During Session**: - Commit after EVERY completed action without asking - Format: `git add -A && git commit -m "[action]: [description]"` - These are checkpoint commits for easy rollback (will be squashed later) - ALWAYS update `claude-notes.md` with session context in every commit ### Rollback & Navigation - **Revert to checkpoint**: bash ```bash git log --oneline -10 # Show recent commits git reset --hard [commit-hash] # Go to specific state ``` - **Forward navigation** (after rollback): bash ```bash git reflog # Shows all commits including "lost" ones git reset --hard [commit-hash] # Jump forward ``` - **Switch between sessions**: bash ```bash git branch --list "claude-session-*" # List all sessions git checkout claude-session-[name] # Resume previous session ``` ### Session Completion - **When feature is complete**: 1. Run quality checks: `pnpm run lint` 2. Squash commits into clean history: bash ```bash git checkout main git merge --squash claude-session-[current] git commit -m "feat: [complete description]" ``` 3. Clean up: `git branch -d claude-session-[current]` - **Abandon session**: bash ```bash git checkout main git branch -D claude-session-[current] # Force delete ``` - **Create PR** (for review): bash ```bash git push origin claude-session-[current] # Then create PR via GitHub/GitLab UI ``` ### Claude Notes Structure The `claude-notes.md` file should maintain: markdown ```markdown # Claude Session: [branch-name] ## Session Info - Started: [timestamp] - Purpose: [main goal] - Status: [active|complete|abandoned] ## Commit History - `[hash]` [action]: [description] - `[hash]` [action]: [description] ## Context - Current task: [what we're working on] - Key decisions: [important choices made] - Dependencies: [relevant files/systems] - Next steps: [planned actions] ## Rollback Points - "Before auth refactor": `abc123` - "Working UI state": `def456`
  1. Automatic branching: Start every session with a new branch
  2. Frequent commits: Checkpoint after each logical change
  3. Context preservation: Update claude-notes.md with every commit
  4. Clean history: Squash merge to main when complete
  5. Easy recovery: Use branch switching instead of dangerous resets on main

bash

# Start new session git checkout -b claude-session-$(date +%Y%m%d-%H%M%S)-[topic] # Checkpoint (use frequently) git add -A && git commit -m "[action]: [what was done]" # Review & rollback git log --oneline -10 git reset --hard [commit] # Complete session git checkout main && git merge --squash claude-session-[name] # List all sessions git branch --list "claude-session-*" # Clean old sessions git branch --merged | grep "claude-session-" | xargs -n 1 git branch -d

bash

# User: "Let's refactor the auth system" git checkout -b claude-session-20241216-143022-refactor-auth echo "Starting auth refactor session" >> claude-notes.md git add -A && git commit -m "init: start auth refactor session" # ... make changes ... git add -A && git commit -m "refactor: extract auth middleware" # User: "Actually, let's go back before that change" git log --oneline -5 git reset --hard HEAD~1 # User: "Looks good, let's finish up" pnpm run lint git checkout main git merge --squash claude-session-20241216-143022-refactor-auth git commit -m "feat: refactor authentication system for better modularity" git branch -d claude-session-20241216-143022-refactor-auth
Read Entire Article