make-branch

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

/make-branch — Move changes from main to a new branch

/make-branch — 将改动从main分支迁移至新分支

When you've been working on
main
(or a random placeholder branch like a petname) and want to PR, this skill creates a well-named feature branch and moves your commits and uncommitted changes onto it.
当你一直在
main
分支(或类似随机占位分支,如自动生成的宠物名分支)上工作,且想要发起PR时,此工具会创建一个命名规范的特性分支,并将你的提交记录和未提交改动迁移至该分支。

Workflow

工作流程

1. Assess the situation

1. 评估当前情况

Run these in parallel:
bash
git branch --show-current
git log origin/main..HEAD --oneline
git diff --stat
git diff --cached --stat
Classify the current branch:
  • main
    /
    master
    — proceed with the branch-out flow (Cases A/B/C below).
  • Placeholder / petname branch — proceed with the in-place rename flow (Case D). A branch is a placeholder if any of these match:
    • Petname pattern:
      ^[a-z]+-[a-z]+$
      (two lowercase words joined by a single hyphen, no slash, no digits — e.g.
      useful-pony
      ,
      sound-midge
      ,
      novel-aphid
      ). Claude Code and similar tools auto-generate these.
    • Generic placeholder name:
      wip
      ,
      tmp
      ,
      temp
      ,
      test
      ,
      scratch
      ,
      work
      , or similar.
    • The user passes an argument asking to rename (e.g. "because this branch is petname", "rename this branch").
  • Already-conventional branch — a branch matching
    ^(feat|fix|refactor|docs|test|chore|perf|ci)/.+
    is well-named. Abort and tell the user to use
    /make-pr
    directly.
  • Protected branches (
    develop
    ,
    dev
    ,
    staging
    ,
    production
    ,
    release/*
    ) — abort; don't touch these.
Abort if there's nothing to move — no commits ahead of
origin/main
and no uncommitted changes (only applies to the branch-out flow; the rename flow can proceed with only committed work).
并行运行以下命令:
bash
git branch --show-current
git log origin/main..HEAD --oneline
git diff --stat
git diff --cached --stat
对当前分支进行分类:
  • main
    /
    master
    — 继续执行分支迁出流程(见下文案例A/B/C)。
  • 占位/宠物名分支 — 执行原地重命名流程(案例D)。若分支符合以下任一特征,则判定为占位分支:
    • 宠物名格式:
      ^[a-z]+-[a-z]+$
      (两个小写单词用单个连字符连接,无斜杠、无数字 — 例如
      useful-pony
      ,
      sound-midge
      ,
      novel-aphid
      )。Claude Code及类似工具会自动生成此类分支名。
    • 通用占位名称:
      wip
      ,
      tmp
      ,
      temp
      ,
      test
      ,
      scratch
      ,
      work
      或类似名称。
    • 用户传入重命名请求参数(例如“因为这是宠物名分支”、“重命名此分支”)。
  • 已符合规范的分支 — 分支名匹配
    ^(feat|fix|refactor|docs|test|chore|perf|ci)/.+
    格式,命名规范。终止操作并告知用户直接使用
    /make-pr
  • 受保护分支
    develop
    ,
    dev
    ,
    staging
    ,
    production
    ,
    release/*
    ) — 终止操作;请勿改动此类分支。
若无内容可迁移则终止 — 若没有领先于
origin/main
的提交记录,且无未提交改动(仅适用于分支迁出流程;重命名流程可仅针对已提交内容执行)。

2. Generate a branch name

2. 生成分支名

Analyze the commits and/or diff to understand the theme of the changes. Generate a branch name:
  • Format:
    <type>/<short-kebab-description>
    (e.g.,
    feat/add-type-annotations
    ,
    fix/url-parsing
    ,
    refactor/simplify-session
    )
  • Types:
    feat
    ,
    fix
    ,
    refactor
    ,
    docs
    ,
    test
    ,
    chore
    ,
    perf
    ,
    ci
  • Keep it under 50 chars, descriptive, no issue numbers unless obvious
分析提交记录和/或差异内容,理解改动主题。生成分支名需遵循:
  • 格式:
    <type>/<short-kebab-description>
    (例如
    feat/add-type-annotations
    ,
    fix/url-parsing
    ,
    refactor/simplify-session
  • 类型:
    feat
    ,
    fix
    ,
    refactor
    ,
    docs
    ,
    test
    ,
    chore
    ,
    perf
    ,
    ci
  • 长度控制在50字符以内,描述清晰,除非明确必要否则不包含问题编号

3. Move changes to the new branch

3. 将改动迁移至新分支

Handle three cases:
Case A: Commits ahead of origin/main + uncommitted changes
bash
git stash --include-untracked
git branch <branch-name>
git reset --hard origin/main
git switch <branch-name>
git stash pop
Case B: Commits ahead of origin/main only (clean working tree)
bash
git branch <branch-name>
git reset --hard origin/main
git switch <branch-name>
Case C: Uncommitted changes only (no commits ahead)
bash
git switch -c <branch-name>
Case D: Already on a placeholder / petname branch — rename in place
Safe, non-destructive. Preserves all commits and working-tree state; no stash needed.
bash
git branch -m <old-placeholder> <branch-name>
If the placeholder branch was already pushed to origin, also update the remote:
bash
git push origin :<old-placeholder> <branch-name>
git push -u origin <branch-name>
处理以下三种情况:
案例A:领先于origin/main的提交记录 + 未提交改动
bash
git stash --include-untracked
git branch <branch-name>
git reset --hard origin/main
git switch <branch-name>
git stash pop
案例B:仅存在领先于origin/main的提交记录(工作树干净)
bash
git branch <branch-name>
git reset --hard origin/main
git switch <branch-name>
案例C:仅存在未提交改动(无领先提交)
bash
git switch -c <branch-name>
案例D:当前已在占位/宠物名分支上 — 原地重命名
安全且无破坏性。保留所有提交记录和工作树状态;无需使用stash。
bash
git branch -m <old-placeholder> <branch-name>
若占位分支已推送到origin,还需更新远程分支:
bash
git push origin :<old-placeholder> <branch-name>
git push -u origin <branch-name>

4. Verify and report

4. 验证并反馈

bash
git branch --show-current
git log origin/main..HEAD --oneline
git status --short
Print a summary:
  • Branch name created
  • Number of commits moved
  • Working tree status
  • Suggest running
    /make-pr
    next
bash
git branch --show-current
git log origin/main..HEAD --oneline
git status --short
输出总结信息:
  • 创建的分支名称
  • 迁移的提交数量
  • 工作树状态
  • 建议下一步执行
    /make-pr

Guidelines

指导原则

  • Always fetch before comparing:
    git fetch origin main
    first
  • Never force-delete or lose commits — the stash+reset approach is safe
  • If
    origin/main
    doesn't exist (e.g., fresh repo), use
    origin/master
    or abort with a message
  • Don't commit uncommitted changes — just move them to the new branch as-is
  • Pick a branch name that would make sense as a PR title prefix
  • 对比前先执行fetch:先运行
    git fetch origin main
  • 绝不强制删除或丢失提交记录 — stash+reset的方式是安全的
  • origin/main
    不存在(例如新仓库),使用
    origin/master
    或终止操作并提示信息
  • 无需提交未改动内容 — 直接将其原样迁移至新分支
  • 选择可作为PR标题前缀的分支名