Skip to content

Commit c3513cc

Browse files
timvwclaude
andcommitted
feat: auto-navigate to main when removing current worktree
When you remove a worktree you're currently in, automatically navigate to the main worktree instead of leaving the shell in a deleted directory. The command now: 1. Detects if current directory is in the worktree being removed 2. Finds the main worktree path (first entry in git worktree list) 3. After successful removal, prints TREE_ME_CD marker to navigate This prevents the confusing situation where your shell is left "in" a non-existent directory after removing the worktree you're in. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 73858a7 commit c3513cc

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

main.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,27 @@ var removeCmd = &cobra.Command{
370370
return fmt.Errorf("no worktree found for branch: %s", branch)
371371
}
372372

373+
// Check if we're currently in the worktree being removed
374+
cwd, err := os.Getwd()
375+
inRemovedWorktree := err == nil && strings.HasPrefix(cwd, existingPath)
376+
377+
// Find the main worktree path (for cd after removal)
378+
var mainWorktreePath string
379+
if inRemovedWorktree {
380+
listCmd := exec.Command("git", "worktree", "list")
381+
output, err := listCmd.Output()
382+
if err == nil {
383+
lines := strings.Split(string(output), "\n")
384+
if len(lines) > 0 {
385+
// First line is always the main worktree
386+
fields := strings.Fields(lines[0])
387+
if len(fields) > 0 {
388+
mainWorktreePath = fields[0]
389+
}
390+
}
391+
}
392+
}
393+
373394
gitCmd := exec.Command("git", "worktree", "remove", existingPath)
374395
gitCmd.Stdout = os.Stdout
375396
gitCmd.Stderr = os.Stderr
@@ -378,6 +399,12 @@ var removeCmd = &cobra.Command{
378399
}
379400

380401
fmt.Printf("✓ Removed worktree: %s\n", existingPath)
402+
403+
// If we were in the removed worktree, navigate to main
404+
if inRemovedWorktree && mainWorktreePath != "" {
405+
printCDMarker(mainWorktreePath)
406+
}
407+
381408
return nil
382409
},
383410
}

0 commit comments

Comments
 (0)