Skip to content

chore(dx): worktree hygiene automation — rebase on main drift #197

@scottschreckengaust

Description

@scottschreckengaust

Problem

Worktrees created from origin/main drift silently as other branches merge to main. By the time a PR is pushed, the branch may be multiple commits behind, causing:

  • Unnecessary merge conflicts at PR time
  • CI running against stale base (masking integration issues)
  • Manual git fetch && git rebase before every push

Example: the fix/194-ts-isolated-modules worktree was 1 commit behind origin/main at push time because PR #171 (ESLint 10) merged after branch creation.

Acceptance criteria

  • Worktrees are created from the latest origin/main (enforce git fetch origin main before git worktree add)
  • Active worktrees are periodically rebased or at minimum warned about staleness
  • No data loss — only rebase clean worktrees (no uncommitted changes)

Suggested implementation

Option A: mise task worktree:sync (recommended)

Add a mise task that iterates active worktrees and rebases them on origin/main:

# mise.toml
[tasks."worktree:sync"]
description = "Fetch origin/main and rebase all active worktrees"
run = """
git fetch origin main
for wt in $(git worktree list --porcelain | grep '^worktree ' | grep -v '$(git rev-parse --show-toplevel)$' | sed 's/^worktree //'); do
  branch=$(git -C "$wt" rev-parse --abbrev-ref HEAD 2>/dev/null)
  if [ -z "$(git -C "$wt" status --porcelain)" ]; then
    echo "Rebasing $branch in $wt..."
    git -C "$wt" rebase origin/main || git -C "$wt" rebase --abort
  else
    echo "SKIP $branch (dirty working tree)"
  fi
done
"""

Option B (optional): SessionStart hook for staleness warning

A Claude Code hook or startup script that checks worktree age and warns:

# Check each worktree's distance from origin/main
for wt in $(git worktree list --porcelain | grep '^worktree ' | sed 's/^worktree //'); do
  behind=$(git -C "$wt" rev-list --count HEAD..origin/main 2>/dev/null)
  if [ "$behind" -gt 0 ]; then
    echo "$wt is $behind commit(s) behind origin/main"
  fi
done

Option C (optional): pre-push rebase

Add to the pre-push hook: if the branch is behind origin/main, auto-rebase before pushing (abort if conflicts).

Labels

enhancement, developer-experience

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions