Skip to content

Latest commit

 

History

History
80 lines (68 loc) · 2.57 KB

File metadata and controls

80 lines (68 loc) · 2.57 KB
name repo-setup
description Clone or refresh a GitHub repo and prepare the working tree. Load this before any situation skill.
license Apache-2.0
metadata
audience
autonomous-agents

Repository Setup

Clone or refresh the repo and create an isolated worktree for the branch.

Why worktrees

The main clone at ~/dev/<owner>/<repo> is shared across sessions. Working directly in it causes concurrent sessions to overwrite each other's changes. Git worktrees give each branch its own directory with its own working tree and index, while sharing the object store.

Steps

  1. Clone (or refresh) the bare-ish main clone:

    gh repo clone <owner>/<repo> ~/dev/<owner>/<repo> -- --depth=50 2>/dev/null || true
    cd ~/dev/<owner>/<repo>
    git fetch --all --prune
  2. Determine the branch name:

    • New issue: issue-<number>-<slug> (e.g. issue-42-fix-login)
    • Existing PR: the PR's head branch — get it with:
      BRANCH=$(gh pr view <number> --json headRefName --jq .headRefName)
  3. Create or reuse a worktree for the branch:

    WORKTREE=~/dev/<owner>/<repo>-wt/<branch>
    • If $WORKTREE already exists, verify it's healthy:
      git -C "$WORKTREE" rev-parse --git-dir >/dev/null 2>&1
      If that fails, remove and recreate:
      git -C ~/dev/<owner>/<repo> worktree remove "$WORKTREE" --force 2>/dev/null || rm -rf "$WORKTREE"
      Then fall through to the new/existing branch creation below. If healthy, cd "$WORKTREE", pull the latest, and skip to step 4:
      git fetch origin
      git reset --hard "origin/<branch>" 2>/dev/null || true
    • New branch (issue):
      DEFAULT_BRANCH=$(gh repo view --json defaultBranchRef --jq .defaultBranchRef.name)
      git worktree add "$WORKTREE" -b <branch> "origin/$DEFAULT_BRANCH"
    • Existing branch (PR checkout):
      git fetch origin "<branch>:<branch>" 2>/dev/null || true
      git worktree add "$WORKTREE" "<branch>"
  4. Work in the worktree — all subsequent commands run from $WORKTREE:

    cd "$WORKTREE"
  5. Clean up (optional, after push): if the worktree is no longer needed:

    git -C ~/dev/<owner>/<repo> worktree remove "$WORKTREE" --force

Important

  • Never git reset --hard or git clean -fd in the main clone — other worktrees depend on the shared objects.
  • The worktree directory pattern ~/dev/<owner>/<repo>-wt/<branch> keeps worktrees adjacent to the main clone without nesting inside it.