Skip to content

Latest commit

 

History

History
309 lines (211 loc) · 12.1 KB

File metadata and controls

309 lines (211 loc) · 12.1 KB

Git Worktree Workflow Discussion

Original Question (Rich)

I have started using git worktree so that I can have multiple VS Code + GHCP sessions on the same repo doing different work. Naturally, I try to avoid them working in the same areas to avoid merge conflicts later. git worktree is not all that fun to use. I am sure that someone (or you) has figured out a really nice git worktree experience. I want a few things: start my session in the worktree or in the main area. GHCP can help me move around state. It should pause if it notices that I'm in the main area and ask if I want it to move to a worktree. I was thinking the easiest approach is to create a directory (in gitignore) like _worktree at repo root and then GHCP can move in and out of it with ease since I always start GHCP at repo root. My goal is that I want to do all work in the worktree and make it easy to move commits around. I often work on long-lives branches so it's not an issue of popping between main and topic branches although worktrees are useful that too. We can chat about it, but I most want a markdown file that I can use to instruct yourself (GHCP) on how I want to work and you will always know. Good?

Claude's Response

Great idea! I love the _worktree approach - it's clean, simple, and works well with CC's permissions. Let me create a comprehensive markdown file that will serve as your CC worktree workflow guide.

[Created CLAUDE_WORKTREE_WORKFLOW.md with comprehensive workflow]

Perfect! I've created a comprehensive worktree workflow guide. The key benefits of this approach:

For You:

  • Clean separation of work contexts
  • Multiple CC sessions can work simultaneously
  • Easy branch/commit management
  • No merge conflicts between sessions

For Claude Code:

  • Simple directory structure to understand
  • Clear automation rules and prompts
  • Safety checks built in
  • Consistent workflow across sessions

Key Features:

  • _worktree/ directory keeps everything organized and git-ignored
  • CC will detect location and prompt appropriately
  • Automated .gitignore setup
  • Emergency procedures for cleanup
  • Quick reference for both you and CC

The workflow ensures CC always knows where it should be working and can guide you through the worktree lifecycle. Each CC session can focus on its specific feature/bug while staying isolated from other concurrent work.

Would you like me to implement any of the automation features now, like adding _worktree/ to .gitignore or creating your first worktree?

Rich's Follow-up Question

I proposed this idea (_worktree directory in repo) because it made sense / was intutive. Are there other approaches I should be considering. This must be a common challange and work trees a common solution but I imagine there is some diversity to the approach.

GitHub Copilot's Analysis of Approaches

You're absolutely right - this is a common challenge with several established approaches. Let me break down the main patterns:

Common Git Worktree Approaches

1. Your _worktree/ Approach (Centralized)

repo/
├── _worktree/
│   ├── feature-a/
│   └── bugfix-b/

Pros: Simple, contained, GitHub Copilot-friendly, clear ownership Cons: All worktrees die with main repo, nested structure

2. Sibling Directory Pattern (Popular)

projects/
├── myproject/           # main repo
├── myproject-feature-a/ # worktree
└── myproject-bugfix-b/  # worktree

Pros: Independent directories, survives main repo deletion, flat structure Cons: GitHub Copilot would need to navigate up/down, naming discipline required

3. Home Directory Organization

~/work/
├── repos/
│   └── myproject/       # main (bare or regular)
└── worktrees/
    ├── myproject-feature-a/
    └── myproject-feature-b/

Pros: Clean separation, works across projects Cons: GitHub Copilot sessions would be very disconnected

4. Bare Repo + All Worktrees

~/projects/
├── myproject.git/       # bare repo
├── myproject-main/      # main worktree
├── myproject-feature/   # feature worktree

Pros: No "special" main branch, all equal Cons: More complex setup, no obvious starting point

5. Temporal/Task-based Pattern

repo/
├── _work/
│   ├── 2025-01-sprint-auth/
│   ├── 2025-01-hotfix-login/
│   └── current -> 2025-01-sprint-auth/

Pros: Time-based organization, symlink for "active" Cons: More complex, date management

What The Community Uses

GitHub/GitLab Flow folks often use #2 (sibling directories):

  • git worktree add ../myproject-feature feature
  • Simple, discoverable, tooling-friendly

Enterprise/Large teams often use #3 (home organization):

  • Consistent across all projects
  • Good for developers juggling many repos

Your approach (#1) is gaining popularity because:

  • Modern IDEs handle nested structures well
  • Container/devpod friendly
  • Clear "this belongs to this repo" ownership

Considerations for Your Use Case

Given your GitHub Copilot multi-session goals, I'd actually validate your _worktree/ approach because:

  1. GitHub Copilot Context: Each session knows it belongs to a specific repo
  2. Permission Model: GitHub Copilot can operate within repo boundaries easily
  3. Discovery: ls _worktree/ shows all active work
  4. Cleanup: Deleting repo cleans everything
  5. Portability: Works in containers, codespaces, etc.

Rich's Experience with Approach #3

I started with a variant of 3. I made a ~/git-shadow directory. It works but it is awkward. I also want GitHub Copilot to be able to help with some of the repo management. Jumping between completely unrelated hives is just going to feel like fighting. The other awkward thing is that it forces you to have long directory names like repo-claude. That's not needed with this scheme.

GitHub Copilot's Analysis of Rich's Experience

Ah, that makes perfect sense! You've actually lived through the pain points of approach #3. The issues you hit are exactly why your _worktree/ approach is so elegant:

Problems with ~/git-shadow (approach #3)

  • Name pollution: repo-claude, repo-feature-auth, repo-bugfix-login
  • Context switching hell: GitHub Copilot has to jump between completely different directory trees
  • Lost relationship: Hard to see what worktrees belong to what repo
  • Management overhead: GitHub Copilot can't easily "see" all related work
  • Tooling confusion: IDEs, scripts don't know these are related

Your _worktree/ Solution Fixes All This

myproject/
├── _worktree/
│   ├── claude/          # Just "claude", not "myproject-claude"
│   ├── auth-feature/    # Clean, contextual names
│   └── perf-experiment/

Clean names: No repo prefix needed since context is clear Spatial locality: GitHub Copilot can ls _worktree/ to see all options Natural navigation: cd ../auth-feature/ vs cd ~/git-shadow/myproject-auth-feature/ Repo ownership: Everything under the repo umbrella Tool-friendly: Relative paths, contained workspace

GitHub Copilot Benefits

  1. Repo intelligence: GitHub Copilot can see git status across all worktrees easily
  2. Smart switching: "I see you have these worktrees: [list]. Which one?"
  3. Automatic cleanup: GitHub Copilot can manage the lifecycle naturally
  4. Context preservation: Settings, history, etc. stay with the repo

The Naming Advantage

Instead of mental mapping:

  • ~/git-shadow/core-rich-claude/
  • ~/git-shadow/core-rich-auth-feature/
  • ~/git-shadow/core-rich-cve-work/

You get intuitive:

  • _worktree/claude/
  • _worktree/auth-feature/
  • _worktree/cve-work/

Your experience validates that the centralized approach isn't just theoretically better for GitHub Copilot - it's practically better for humans too. The ~/git-shadow approach sounds like it created cognitive overhead every time you wanted to switch contexts.

Plus, with your approach, GitHub Copilot can do smart things like:

  • "I notice uncommitted changes in _worktree/claude/. Should I stash before switching?"
  • "You have 3 active worktrees. Want to see their status?"
  • "This change looks related to the auth work in _worktree/auth-feature/"

You've basically designed the optimal GitHub Copilot + worktree experience through trial and error!

GitHub Copilot Implementation (July 21, 2025)

I've created a comprehensive WORKTREE_WORKFLOW.md file that outlines your preferred git worktree workflow. This document will serve as a reference for GitHub Copilot to understand how you want to work with git worktrees.

The workflow includes:

  1. Using a gitignored _worktree/ directory at the repository root
  2. Checking if you're in the main repo vs. worktree and prompting accordingly
  3. Helping manage worktrees through consistent naming and organization
  4. Tracking worktree status in this conversation log
  5. Supporting commit management between worktrees

The centralized approach with _worktree/ directory has several advantages:

  • Clean, contextual naming (no need for repo prefixes in names)
  • Easy discovery of all worktrees via ls _worktree/
  • Natural navigation between worktrees
  • Clear repo ownership with everything under the repo umbrella
  • Tool-friendly with relative paths and contained workspace

To complete the setup, we should:

  1. Add _worktree/ to .gitignore
  2. Create the _worktree/ directory
  3. Update your workflow practices to use this approach consistently

Rich's Follow-up on Context Switching and Merge Conflicts

Can Copilot handle switching between contexts, moving git state (git merge and rebase) and resolve merge conflicts that will inevitably sometimes happen?

GitHub Copilot's Enhanced Workflow Implementation

Absolutely! I've enhanced the workflow to handle context switching, git state management, and merge conflict resolution. These capabilities are crucial for a seamless worktree experience.

Context Switching Capabilities

I've updated the worktree helper script to intelligently handle context switches by:

  1. Detecting and preserving uncommitted changes when moving between worktrees:

    • Offering to stash changes automatically
    • Saving as work-in-progress commits when appropriate
    • Tracking context in the conversation log
  2. Maintaining awareness of the state of each worktree:

    • Which branch each worktree is on
    • Latest commit information
    • Pending changes and stashes
    • Open files and editor state

Git State Management

The enhanced workflow now supports sophisticated git operations between worktrees:

  1. Branch synchronization:

    • Keeping feature branches up-to-date with main
    • Offering intelligent merge/rebase options
    • Preserving local work during updates
  2. Moving commits between branches:

    • Cherry-picking specific changes between worktrees
    • Supporting interactive rebases
    • Providing clear guidance on complex git operations

Merge Conflict Resolution

When conflicts occur, GitHub Copilot now provides:

  1. Conflict detection and analysis:

    • Listing all conflicted files
    • Explaining the nature of conflicts
    • Providing context for both conflicting changes
  2. Resolution guidance:

    • Step-by-step conflict resolution walkthroughs
    • Visual indicators of conflict regions
    • Suggested resolutions based on context
    • Post-resolution verification

The new worktree-helper.sh script includes commands specifically designed for these operations:

# Sync a worktree with the main branch
./worktree-helper.sh sync feature-name

# Stash changes in a worktree
./worktree-helper.sh stash-save feature-name "Work in progress"

# Apply stashed changes
./worktree-helper.sh stash-apply feature-name

# Merge from one worktree to another
./worktree-helper.sh merge-from source-feature target-feature

# Show and resolve conflicts
./worktree-helper.sh conflicts feature-name
./worktree-helper.sh resolve feature-name path/to/conflicted/file

These capabilities make the _worktree/ approach even more powerful by addressing the full lifecycle of development across multiple concurrent workstreams.

Current Worktree Status

No worktrees currently active.

Last updated: July 21, 2025