Skip to content

feat: support rebasing branches checked out in worktrees#145

Open
abersnaze wants to merge 1 commit intomsiemens:masterfrom
abersnaze:worktree-support
Open

feat: support rebasing branches checked out in worktrees#145
abersnaze wants to merge 1 commit intomsiemens:masterfrom
abersnaze:worktree-support

Conversation

@abersnaze
Copy link
Copy Markdown

Problem

When a branch is checked out in a separate git worktree, git up fails with:

fatal: '<branch>' is already used by worktree at '<path>'

This happens because rebase_all_branches() tries to git checkout the branch, which git refuses since it's already checked out in another worktree.

Solution

Before attempting checkout, build a map of branches to their worktree paths via git worktree list --porcelain. For branches found in worktrees:

  • Fast-forward: run git merge --ff-only directly in the worktree
  • Rebase: stash worktree changes if dirty, rebase in the worktree, then unstash

This matches the existing behavior for non-worktree branches — failed rebases are left for the user to resolve.

Changes

  • gitup.py: Added _build_worktree_map() and _rebase_in_worktree() methods; modified rebase_all_branches() to dispatch worktree branches accordingly
  • test_worktree.py: Tests both fast-forward and rebase scenarios with a worktree branch

Testing

All 37 tests pass (36 existing + 1 new).


Warp conversation

Co-Authored-By: Oz oz-agent@warp.dev

When a branch is checked out in a separate git worktree, `git checkout`
fails with 'already used by worktree'. Instead of checking out such
branches in the main repo, detect them via `git worktree list --porcelain`
and perform the rebase (or fast-forward merge) directly in the worktree
directory.

This handles:
- Fast-forward: runs `git merge --ff-only` in the worktree
- Rebase: stashes worktree changes if dirty, rebases, then unstashes

Fixes the error:
  fatal: '<branch>' is already used by worktree at '<path>'

Co-Authored-By: Oz <oz-agent@warp.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant