using-git-worktrees

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Using Git Worktrees (Simplified: Branch-Only Mode)

使用Git Worktrees(简化版:仅分支模式)

Overview

概述

Simplified approach: Create a temporary branch in the current working directory instead of a separate worktree.
Core principle: New branch + safety verification = isolated implementation.
Announce at start: "I'm creating a temporary branch for isolated implementation work."
简化方法: 在当前工作目录中创建临时分支,而非单独的工作树。
核心原则: 新分支 + 安全验证 = 隔离式实现。
启动时告知: "我正在创建一个临时分支用于隔离式实现工作。"

Branch Creation Process

分支创建流程

1. Generate Branch Name

1. 生成分支名称

Use descriptive naming based on the task:
bash
undefined
根据任务使用描述性命名:
bash
undefined

Format: feature/<task-name> or fix/<issue-name>

格式: feature/<任务名称> 或 fix/<问题名称>

branch_name="feature/$(echo "$TASK" | tr ' ' '-' | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9-]//g' | cut -c1-50)"
undefined
branch_name="feature/$(echo "$TASK" | tr ' ' '-' | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9-]//g' | cut -c1-50)"
undefined

2. Verify Clean Working Directory

2. 验证工作目录是否干净

bash
undefined
bash
undefined

Check for uncommitted changes

检查未提交的更改

git status --porcelain

**If uncommitted changes exist:**
- Ask user to stash or commit first
- OR create branch anyway and warn about carrying over changes
git status --porcelain

**如果存在未提交更改:**
- 请用户先暂存或提交
- 或者直接创建分支并警告会携带现有更改

3. Create and Switch to Branch

3. 创建并切换到分支

bash
undefined
bash
undefined

Create new branch from current HEAD

从当前HEAD创建新分支

git checkout -b "$branch_name"
undefined
git checkout -b "$branch_name"
undefined

4. Verify Branch Created

4. 验证分支已创建

bash
undefined
bash
undefined

Confirm current branch

确认当前分支

git branch --show-current

**Expected:** Should output the new branch name.
git branch --show-current

**预期结果:** 应输出新分支名称。

5. Report Ready

5. 报告准备就绪

Temporary branch created: $branch_name
Ready to implement: $TASK
临时分支已创建:$branch_name
可开始实现:$TASK

Quick Reference

快速参考

SituationAction
Clean working directoryCreate branch directly
Uncommitted changesStash/commit first OR warn and proceed
Branch existsAppend timestamp or ask for different name
场景操作
工作目录干净直接创建分支
存在未提交更改先暂存/提交 或 警告后继续
分支已存在添加时间戳或询问其他名称

Common Mistakes

常见错误

Skipping clean directory check

跳过工作目录检查

  • Problem: Carries over unrelated changes to new branch
  • Fix: Always check
    git status --porcelain
    first
  • 问题: 将无关更改带入新分支
  • 解决方法: 始终先检查
    git status --porcelain

Not verifying branch creation

未验证分支创建

  • Problem: May still be on wrong branch
  • Fix: Always confirm with
    git branch --show-current
  • 问题: 可能仍处于错误分支
  • 解决方法: 始终用
    git branch --show-current
    确认

Example Workflow

示例工作流

You: I'm using the using-git-worktrees skill to create a temporary branch.

[Check git status - clean]
[Create branch: git checkout -b feature/subscription-user-id-uuid]
[Verify: git branch --show-current outputs "feature/subscription-user-id-uuid"]

Temporary branch created: feature/subscription-user-id-uuid
Ready to implement: Fix SubscriptionPlan user_id from int to UUID
你:我正在使用using-git-worktrees技能创建临时分支。

[检查git状态 - 干净]
[创建分支:git checkout -b feature/subscription-user-id-uuid]
[验证:git branch --show-current输出"feature/subscription-user-id-uuid"]

临时分支已创建:feature/subscription-user-id-uuid
可开始实现:修复SubscriptionPlan的user_id从int改为UUID

Red Flags

注意事项

Never:
  • Skip working directory check
  • Proceed without verifying branch was created
  • Create branch with uncommitted changes without warning
Always:
  • Verify clean working directory first
  • Confirm branch creation
  • Use descriptive branch names
绝对不要:
  • 跳过工作目录检查
  • 未验证分支创建就继续
  • 在未警告的情况下带未提交更改创建分支
始终要:
  • 先验证工作目录是否干净
  • 确认分支已创建
  • 使用描述性分支名称

Integration

集成

Called by:
  • brainstorming (Phase 4) - REQUIRED when design is approved and implementation follows
  • subagent-driven-development - REQUIRED before executing any tasks
  • executing-plans - REQUIRED before executing any tasks
  • Any skill needing isolated workspace
Pairs with:
  • finishing-a-development-branch - REQUIRED for cleanup after work complete
被以下技能调用:
  • brainstorming(第4阶段)——设计获批后进入实现阶段时必须调用
  • subagent-driven-development——执行任何任务前必须调用
  • executing-plans——执行任何任务前必须调用
  • 任何需要隔离工作区的技能
搭配使用:
  • finishing-a-development-branch——工作完成后清理时必须调用