Skip to content

fix(finish): cd to main repo before worktree removal#952

Open
JasonOA888 wants to merge 1 commit intoobra:mainfrom
JasonOA888:fix/issue-238-worktree-cleanup-cwd
Open

fix(finish): cd to main repo before worktree removal#952
JasonOA888 wants to merge 1 commit intoobra:mainfrom
JasonOA888:fix/issue-238-worktree-cleanup-cwd

Conversation

@JasonOA888
Copy link
Copy Markdown

Summary

Fixes #238

Problem

The /finish workflow's Step 5 runs git worktree remove <path> without first changing the working directory. When the current shell session is inside the worktree being removed, this fails:

fatal: cannot remove worktree '<path>': '<path>' is the current working directory

This is a common scenario when Claude Code's shell is still inside the feature worktree.

Solution

Before running git worktree remove, change directory to the main repository:

# Get main repo path (first entry in worktree list is always main)
main_repo=$(git worktree list | head -1 | awk '{print $1}')

# Change to main repo before removal
cd "$main_repo"

# Now safe to remove
git worktree remove <worktree-path>

Testing

  • Verified that git worktree list | head -1 | awk '{print $1}' correctly returns the main repo path
  • Removal succeeds after cd-ing out of the worktree

Files Changed

  • skills/finishing-a-development-branch/SKILL.md — Updated Step 5 with directory change

When the current working directory is inside a worktree, `git worktree remove`
fails with:

  fatal: cannot remove worktree '<path>': '<path>' is the current working directory

This happens when Claude Code's shell session is still inside the worktree
being cleaned up.

Fix: Before removing the worktree, change directory to the main repository
(the first entry in `git worktree list`). This ensures the removal command
runs from outside the target worktree.

Fixes obra#238
@obra obra added bug Something isn't working worktrees Git worktree management pr-template-rules-ignored PR filed after template was added but didn't use it labels Mar 29, 2026
Copy link
Copy Markdown

@robotsnh robotsnh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fine. My only concern is what would happen if the user's repo isn't on git, but there's already a git command in that code block, so that's a problem for another PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working pr-template-rules-ignored PR filed after template was added but didn't use it worktrees Git worktree management

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: Worktree cleanup fails when working directory is inside worktree

4 participants