agentleFS
lvdaxianerlvdaxianer/skills/git-branch-worktree-convention/SKILL.md

git-branch-worktree-convention

Skill0 starsChanged 30 days agoApache-2.0View on GitHub

Project-level task branch and worktree convention. Use whenever creating or validating task branches, selecting a change integration branch, recording SOURCE_BRANCH, merging a completed task, or protecting the original checkout.

The file

Show its rules and text

We split this skill into the rules it gives an agent

32 rules. About 0 are common, 20 are distinctive, and 12 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈20

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 12

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use one confirmed integration branch for a change, then create every task branch from that branch and merge every completed task back into …

    line 8
  2. Distinctive:

    Follow the more specific authority when instructions overlap.

    line 42
  3. Distinctive:

    Read the checked-out branch with git branch --show-current.

    line 51
  4. Very specific:

    Stop if Git reports a detached HEAD.

    line 52
  5. Very specific:

    Ask the user: 当前分支 <branch>,是否以它作为本次 change 的集成分支?

    line 53
  6. Very specific:

    After confirmation, record that exact branch name as INTEGRATION_BRANCH in the current change/task context.

    line 54
  7. Very specific:

    Every task branch for the change starts from INTEGRATION_BRANCH.

    line 62
  8. Distinctive:

    Every completed task merges back into that same branch.

    line 63
  9. Very specific:

    Before starting the next task, switch to INTEGRATION_BRANCH.

    line 64
  10. Distinctive:

    Do not replace the integration branch because the operator happens to switch branches mid-change.

    line 66
  11. Distinctive:

    Use one task branch per OpenSpec task or other independently verifiable task.

    line 72
  12. Very specific:

    All tasks belonging to the same change use the same <change-id> and the same INTEGRATION_BRANCH.

    line 95
  13. Very specific:

    Do not use 📦 parent branches.

    line 97
  14. Distinctive:

    Before creating a task branch, confirm it matches the regex and that no local branch or worktree already uses the name.

    line 105
  15. Distinctive:

    All task worktrees live under:

    line 110
  16. Distinctive:

    Run these commands from the clean checkout of the confirmed integration branch:

    line 140
  17. Very specific:

    All task edits, tests, and commits occur inside TASK_WORKTREE.

    line 179
  18. Very specific:

    Read .source_branch from the task worktree instead of guessing the target.

    line 184
  19. Distinctive:

    If the integration checkout has unrelated uncommitted changes or the merge conflicts, stop.

    line 199
  20. Distinctive:

    Only after the ancestry check succeeds and the task worktree is clean:

    line 203
  21. Very specific:

    Before creating task T2:

    line 220
  22. Distinctive:

    Verify it is clean and contains the previous task merge.

    line 223
  23. Very specific:

    Create T2 from INTEGRATION_BRANCH, not from T1 and not from an older commit.

    line 224
  24. Distinctive:

    Read code and Git history.

    line 245
  25. Distinctive:

    Run task tests that write build, coverage, or log artifacts.

    line 253
  26. Distinctive:

    All task implementation and verification runs in the task worktree.

    line 256
  27. Distinctive:

    Use a task branch and dedicated worktree for every independently verifiable implementation task.

    line 297
  28. Distinctive:

    Create every task branch from the confirmed integration branch.

    line 301
  29. Distinctive:

    Start each later task from the integration branch after prior task merges.

    line 303
  30. Distinctive:

    Do not create or use parent aggregation branches.

    line 304
  31. Distinctive:

    Do not edit task files or create task commits from the original checkout.

    line 305
  32. Distinctive:

    Do not delete a task worktree or branch until its merge is verified by the ancestry check.

    line 306

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Git Branch and Worktree Convention

Use one confirmed integration branch for a change, then create every task branch from that branch and merge every completed task back into it. dev is only an example: the integration branch is the branch the user confirms when the first task begins.

This skill defines branch names, source-branch traceability, and integration rules. using-git-worktrees owns safe worktree creation mechanics; development-workflow owns when each gate occurs; and commit owns commit message format.

Model

confirmed integration branch (for example: dev)
  ├─ task 1 branch -> verify -> merge back into dev
  ├─ task 2 branch -> verify -> merge back into dev
  └─ task N branch -> verify -> merge back into dev

end state: remain on dev, which contains every merged task

There is no parent/change branch. Do not create a branch merely to aggregate task branches. The integration branch is the only aggregation point.

Authority and Conflicts

ConcernAuthoritative source
Task branch name, integration source, SOURCE_BRANCH, merge target, original-checkout protectionThis skill
Worktree creation safety and baseline verificationusing-git-worktrees
Workflow timing, task gates, and final integrationdevelopment-workflow
Commit message formatcommit
Force-push, reset, hook skipping, and destructive Git operationsProject/global Git rules

Follow the more specific authority when instructions overlap. Do not duplicate or weaken another skill's safety rules.

Integration Branch

Select it once

At the first task of a change:

  1. Read the checked-out branch with git branch --show-current.
  2. Stop if Git reports a detached HEAD.
  3. Ask the user: 当前分支 <branch>,是否以它作为本次 change 的集成分支?
  4. After confirmation, record that exact branch name as INTEGRATION_BRANCH in the current change/task context.

The confirmed branch can be dev, release/*, main, or another valid working branch. Never assume that a branch literally named dev exists.

Keep it fixed for the change

  • Every task branch for the change starts from INTEGRATION_BRANCH.
  • Every completed task merges back into that same branch.
  • Before starting the next task, switch to INTEGRATION_BRANCH; it now includes the preceding task's merge.
  • Do not replace the integration branch because the operator happens to switch branches mid-change. Ask before changing it; changing it starts a new integration boundary.

Task Branch Naming

Use one task branch per OpenSpec task or other independently verifiable task.

features/✅<change-id>-T<task-num>--<task中文名>
fixes/✅<change-id>-T<task-num>--<子问题中文名>

Examples:

features/✅CHG-001-T1--支付模块改造
features/✅CHG-001-T2--库存扣减逻辑
fixes/✅BUG-055-T1--添加乐观锁

Rules:

  • ✅ (U+2705) identifies a task branch.
  • <change-id> is uppercase letters, a hyphen, then digits, such as CHG-001 or BUG-055.
  • -T<task-num> identifies the task number.
  • The Chinese task description contains at least three Chinese characters and has no spaces or shell-special characters.
  • All tasks belonging to the same change use the same <change-id> and the same INTEGRATION_BRANCH.
  • Do not use 📦 parent branches.

Validation regex:

^(features|fixes)/✅[A-Z]+-[0-9]+-T[0-9]+--[一-龥]{3,}$

Before creating a task branch, confirm it matches the regex and that no local branch or worktree already uses the name.

Worktree and Source Recording

All task worktrees live under:

.claude/worktrees/<full-task-branch-name>/

The worktree root must be ignored by Git before creation. Use the safety and directory checks from using-git-worktrees before running the commands below.

Record the merge target before any task-owned work begins. In the task worktree, create .source_branch containing exactly:

SOURCE_BRANCH=<INTEGRATION_BRANCH>

For example, a task created from dev contains:

SOURCE_BRANCH=dev

.source_branch is local worktree metadata. It tells the completion step the only branch that may receive this task's merge.

Create a Task Branch and Worktree

git worktree add -b creates and checks out the branch in one operation. It is the worktree-safe equivalent of git checkout -b <task-branch>.

Run these commands from the clean checkout of the confirmed integration branch:

INTEGRATION_BRANCH=$(git branch --show-current)
TASK_BRANCH='features/✅CHG-001-T1--支付模块改造'
TASK_WORKTREE=".claude/worktrees/$TASK_BRANCH"

# Confirm INTEGRATION_BRANCH with the user before the first task.
test -n "$INTEGRATION_BRANCH" || { echo 'detached HEAD'; exit 1; }
test -z "$(git status --porcelain)" || { echo 'integration checkout is dirty'; exit 1; }
git show-ref --verify "refs/heads/$INTEGRATION_BRANCH"
if git show-ref --verify --quiet "refs/heads/$TASK_BRANCH"; then
  echo "task branch already exists: $TASK_BRANCH"
  exit 1
fi
test ! -e "$TASK_WORKTREE" || exit 1

git worktree add "$TASK_WORKTREE" -b "$TASK_BRANCH" "$INTEGRATION_BRANCH"

Then, before the first edit or commit, write the source record in the new worktree:

TASK_GIT_DIR=$(git -C "$TASK_WORKTREE" rev-parse --git-dir)
mkdir -p "$TASK_GIT_DIR/info"
printf '.source_branch\n' >> "$TASK_GIT_DIR/info/exclude"
printf 'SOURCE_BRANCH=%s\n' "$INTEGRATION_BRANCH" > "$TASK_WORKTREE/.source_branch"

Verify the setup:

git worktree list --porcelain
git -C "$TASK_WORKTREE" branch --show-current
git -C "$TASK_WORKTREE" status --short
cat "$TASK_WORKTREE/.source_branch"

All task edits, tests, and commits occur inside TASK_WORKTREE.

Merge a Completed Task

Merge only after the task's required tests, review, and workflow gates pass. Read .source_branch from the task worktree instead of guessing the target.

TASK_WORKTREE='.claude/worktrees/features/✅CHG-001-T1--支付模块改造'
TASK_BRANCH=$(git -C "$TASK_WORKTREE" branch --show-current)
INTEGRATION_BRANCH=$(cut -d= -f2 "$TASK_WORKTREE/.source_branch")

# Use the clean checkout/worktree where the integration branch is checked out.
git switch "$INTEGRATION_BRANCH"
test -z "$(git status --porcelain)" || { echo 'integration checkout is dirty'; exit 1; }
git show-ref --verify "refs/heads/$INTEGRATION_BRANCH"
git merge --no-ff "$TASK_BRANCH"
git merge-base --is-ancestor "$TASK_BRANCH" "$INTEGRATION_BRANCH"

If the integration checkout has unrelated uncommitted changes or the merge conflicts, stop. Preserve those changes and resolve the issue before claiming the task integrated.

Only after the ancestry check succeeds and the task worktree is clean:

test -z "$(git -C "$TASK_WORKTREE" status --porcelain)" || {
  echo 'task worktree is dirty'
  exit 1
}
git worktree remove "$TASK_WORKTREE"
git branch -d "$TASK_BRANCH"
git switch "$INTEGRATION_BRANCH"

The final git switch is intentional: after every task, and after the last task, remain on the confirmed integration branch.

Start the Next Task

Before creating task T2:

  1. Return to INTEGRATION_BRANCH.
  2. Verify it is clean and contains the previous task merge.
  3. Create T2 from INTEGRATION_BRANCH, not from T1 and not from an older commit.
  4. Record the same SOURCE_BRANCH=<INTEGRATION_BRANCH> in T2's worktree.
  5. Repeat the normal test, review, merge, and cleanup gates.

This makes the dependency chain explicit:

integration branch at start
  -> task T1 -> merge into integration branch
  -> task T2 starts from updated integration branch
  -> merge into integration branch

Original Checkout Protection

Once a task worktree exists, the checkout where the integration branch is checked out is read-only for that task.

Allowed there:

  • Read code and Git history.
  • Inspect status, logs, diffs, and blame.
  • Switch to the integration branch for the verified merge and cleanup.

Forbidden there:

  • Edit task-owned files.
  • Create task commits.
  • Run task tests that write build, coverage, or log artifacts.
  • Change task-related dependencies or configuration.

All task implementation and verification runs in the task worktree. Confirm the current directory before task work:

git rev-parse --path-format=relative --git-common-dir 2>/dev/null | grep -q worktrees

An exit code of 0 means the command runs from a linked worktree.

Validation Checklists

Before the first task

  • The current checkout is on a named branch, not detached HEAD.
  • The user confirmed the current branch as INTEGRATION_BRANCH.
  • The integration checkout is clean.
  • The task branch name matches the task regex.
  • No existing branch or worktree has the task name.
  • .claude/worktrees/ is ignored.

Before each task

  • Current branch is the confirmed INTEGRATION_BRANCH.
  • The integration checkout is clean.
  • The task branch is created from the current INTEGRATION_BRANCH tip.
  • The task worktree records SOURCE_BRANCH=<INTEGRATION_BRANCH>.
  • Edits, tests, and commits run only in the task worktree.

Before task cleanup

  • The task's focused and broader verification gates passed.
  • The task worktree is clean.
  • .source_branch identifies an existing integration branch.
  • The integration checkout is clean before merging.
  • git merge --no-ff <task-branch> completed successfully.
  • git merge-base --is-ancestor <task-branch> <integration-branch> succeeds.
  • The task worktree and branch are removed only after the successful merge.
  • The final checkout is INTEGRATION_BRANCH.

Non-Negotiable Rules

  • Use a task branch and dedicated worktree for every independently verifiable implementation task.
  • Confirm the integration branch once at the beginning of a change; do not silently change it later.
  • Create every task branch from the confirmed integration branch.
  • Merge every task only into the SOURCE_BRANCH recorded in its worktree.
  • Start each later task from the integration branch after prior task merges.
  • Do not create or use parent aggregation branches.
  • Do not edit task files or create task commits from the original checkout.
  • Do not delete a task worktree or branch until its merge is verified by the ancestry check.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.