branch

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Branch

分支

Start work on a new git branch, optionally in a new worktree.
创建新的Git分支,也可选择在新工作区中创建。

Arguments

参数

Accepts one of the following as arguments:
  • Issue number: e.g.,
    /branch 123
    — fetches the issue title and derives the branch name
  • Description: e.g.,
    /branch add shellcheck support
    — derives the branch name from the text
  • Worktree flag: append
    --worktree
    or
    -w
    to create a git worktree instead of a regular branch
Examples:
  • /branch 123
    — branch from issue #123
  • /branch add dark mode toggle
    — branch from description
  • /branch 123 --worktree
    — worktree from issue #123
  • /branch fix login redirect -w
    — worktree from description
接受以下任意一种作为参数:
  • 问题编号:例如,
    /branch 123
    —— 获取问题标题并生成分支名称
  • 描述文本:例如,
    /branch add shellcheck support
    —— 根据文本内容生成分支名称
  • 工作区标记:追加
    --worktree
    -w
    以创建Git工作区,而非普通分支
示例:
  • /branch 123
    —— 基于编号#123的问题创建分支
  • /branch add dark mode toggle
    —— 根据描述文本创建分支
  • /branch 123 --worktree
    —— 基于编号#123的问题创建工作区
  • /branch fix login redirect -w
    —— 根据描述文本创建工作区

Branch Naming

分支命名规则

Derive the branch name using this format:
text
<type>/<issue-number?>-<slug>
  • type: Infer from context —
    feat/
    ,
    fix/
    ,
    chore/
    ,
    docs/
    ,
    refactor/
    ,
    perf/
    ,
    test/
    ,
    ci/
    • If an issue has labels like
      bug
      fix/
      ,
      enhancement
      feat/
      ,
      documentation
      docs/
    • If a description starts with a semantic prefix, use it
    • Default to
      feat/
      if unclear
  • issue-number: Include only if an issue number was provided
  • slug: Lowercase, hyphenated, max ~50 chars, derived from the issue title or description
    • Strip filler words when too long
    • e.g., "Add shellcheck support for shell scripts" →
      add-shellcheck-support
Examples:
  • Issue #123 titled "feat(cli): add watch mode" →
    feat/123-add-watch-mode
  • Issue #45 labeled
    bug
    , titled "Parser crashes on empty input" →
    fix/45-parser-crashes-on-empty-input
  • Description "add dark mode" →
    feat/add-dark-mode
  • Description "fix: resolve null pointer" →
    fix/resolve-null-pointer
分支名称采用以下格式生成:
text
<type>/<issue-number?>-<slug>
  • type:根据上下文推断 ——
    feat/
    ,
    fix/
    ,
    chore/
    ,
    docs/
    ,
    refactor/
    ,
    perf/
    ,
    test/
    ,
    ci/
    • 如果问题带有
      bug
      标签 → 使用
      fix/
      enhancement
      标签 → 使用
      feat/
      documentation
      标签 → 使用
      docs/
    • 如果描述文本以语义化前缀开头,则直接使用该前缀
    • 若无法明确推断,默认使用
      feat/
  • issue-number:仅在提供问题编号时包含
  • slug:由问题标题或描述文本生成,采用小写连字符格式,最长约50字符
    • 若内容过长,去除冗余填充词
    • 示例:"Add shellcheck support for shell scripts" →
      add-shellcheck-support
示例:
  • 编号#123、标题为"feat(cli): add watch mode"的问题 →
    feat/123-add-watch-mode
  • 带有
    bug
    标签、标题为"Parser crashes on empty input"的问题 →
    fix/45-parser-crashes-on-empty-input
  • 描述文本"add dark mode" →
    feat/add-dark-mode
  • 描述文本"fix: resolve null pointer" →
    fix/resolve-null-pointer

Usage — Regular Branch

使用方式 —— 普通分支

When asked to start a new branch (no
--worktree
/
-w
flag):
  1. Ensure clean state:
    • Run
      git status
      — if there are uncommitted changes, warn the user and stop
    • Do NOT stash or discard changes automatically
  2. Fetch latest:
    bash
    git fetch origin
  3. Resolve branch name:
    • If an issue number was given, fetch it:
      gh issue view <number> --json title,labels
    • Derive the branch name per the naming rules above
  4. Create and switch to the branch:
    bash
    git checkout -b <branch-name> origin/main
  5. Confirm — Print the branch name and a summary of what was done
当需要创建普通分支(不带
--worktree
/
-w
标记)时:
  1. 确保工作区干净
    • 执行
      git status
      —— 如果存在未提交的更改,向用户发出警告并终止操作
    • 切勿自动暂存或丢弃更改
  2. 拉取最新代码
    bash
    git fetch origin
  3. 确定分支名称
    • 如果提供了问题编号,获取问题信息:
      gh issue view <number> --json title,labels
    • 根据上述命名规则生成分支名称
  4. 创建并切换至分支
    bash
    git checkout -b <branch-name> origin/main
  5. 确认操作 —— 打印分支名称及操作摘要

Usage — Worktree

使用方式 —— 工作区

When
--worktree
or
-w
is specified:
  1. Ensure clean state:
    • Run
      git status
      — if there are uncommitted changes, warn the user and stop
  2. Fetch latest:
    bash
    git fetch origin
  3. Resolve branch name (same rules as above)
  4. Determine worktree path:
    • Place as a sibling directory to the current repo
    • Format:
      <repo-dir>-<slug>
      (without the type prefix)
    • e.g., if repo is at
      ~/Code/py-lintro
      and branch is
      feat/123-add-watch-mode
      :
      • Worktree path:
        ~/Code/py-lintro-123-add-watch-mode
    • e.g., if branch is
      fix/parser-crash
      :
      • Worktree path:
        ~/Code/py-lintro-parser-crash
  5. Create the worktree:
    bash
    git worktree add -b <branch-name> <worktree-path> origin/main
  6. Confirm — Print the worktree path, branch name, and how to navigate there:
    text
    Created worktree at ~/Code/py-lintro-123-add-watch-mode
    Branch: feat/123-add-watch-mode (from origin/main)
    
    To start working:
      cd ~/Code/py-lintro-123-add-watch-mode
当指定
--worktree
-w
标记时:
  1. 确保工作区干净
    • 执行
      git status
      —— 如果存在未提交的更改,向用户发出警告并终止操作
  2. 拉取最新代码
    bash
    git fetch origin
  3. 确定分支名称(规则同上)
  4. 确定工作区路径
    • 工作区目录与当前仓库同级
    • 格式:
      <repo-dir>-<slug>
      (不含type前缀)
    • 示例:若仓库路径为
      ~/Code/py-lintro
      ,分支名称为
      feat/123-add-watch-mode
      • 工作区路径:
        ~/Code/py-lintro-123-add-watch-mode
    • 示例:若分支名称为
      fix/parser-crash
      • 工作区路径:
        ~/Code/py-lintro-parser-crash
  5. 创建工作区
    bash
    git worktree add -b <branch-name> <worktree-path> origin/main
  6. 确认操作 —— 打印工作区路径、分支名称及导航指引:
    text
    Created worktree at ~/Code/py-lintro-123-add-watch-mode
    Branch: feat/123-add-watch-mode (from origin/main)
    
    To start working:
      cd ~/Code/py-lintro-123-add-watch-mode

Important

注意事项

  • NEVER force-delete branches or worktrees
  • NEVER stash or discard uncommitted changes — always warn and stop
  • Always branch from
    origin/main
    (freshly fetched)
  • If the branch name already exists, warn the user and ask what to do
  • 切勿强制删除分支或工作区
  • 切勿暂存或丢弃未提交的更改 —— 始终发出警告并终止操作
  • 始终基于
    origin/main
    (拉取最新版本后)创建分支
  • 如果分支名称已存在,向用户发出警告并询问后续操作