create-branch

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Create Branch

创建分支

Create a git branch with the correct type prefix and a descriptive name following Sentry conventions.
遵循Sentry规范创建带有正确类型前缀和描述性名称的git分支。

Step 1: Get the Username Prefix

步骤1:获取用户名前缀

Run
gh api user --jq .login
to get the GitHub username.
If the command fails (e.g. not authenticated), ask the user for their preferred prefix.
运行
gh api user --jq .login
获取GitHub用户名。
如果命令执行失败(例如未完成身份验证),询问用户偏好的前缀。

Step 2: Determine the Branch Description

步骤2:确定分支描述

If
$ARGUMENTS
is provided
, use it as the description of the work.
If no arguments, check for local changes:
bash
git diff
git diff --cached
git status --short
  • Changes exist: read the diff content to understand what the work is about and generate a description.
  • No changes: ask the user what they are about to work on.
如果提供了
$ARGUMENTS
,将其作为工作内容的描述。
如果没有传入参数,检查本地变更:
bash
git diff
git diff --cached
git status --short
  • 存在变更:读取diff内容了解工作内容,生成描述。
  • 无变更:询问用户将要开展的工作内容。

Step 3: Classify the Type

步骤3:分类确定类型

Pick the type from this table based on the description:
TypeUse when
feat
New user-facing functionality
fix
Broken behavior now works
ref
Same behavior, different structure
chore
Deps, config, version bumps, updating existing tooling — no new logic
perf
Same behavior, faster
style
CSS, formatting, visual-only
docs
Documentation only
test
Tests only
ci
CI/CD config
build
Build system
meta
Repo metadata changes
license
License changes
When unsure:
feat
for new things (including new scripts, skills, or tools),
ref
for restructuring existing things,
chore
only when updating/maintaining something that already exists.
根据描述从下表中选择分支类型:
类型使用场景
feat
面向用户的新功能
fix
修复异常行为,使其恢复正常工作
ref
业务行为不变,调整代码结构
chore
依赖、配置、版本升级、更新现有工具 —— 无新增业务逻辑
perf
业务行为不变,提升性能
style
仅涉及CSS、格式、视觉类改动
docs
仅涉及文档改动
test
仅涉及测试改动
ci
CI/CD配置改动
build
构建系统改动
meta
仓库元数据改动
license
许可证改动
无法确定类型时:新增内容(包括新脚本、skill、工具)使用
feat
,调整现有内容结构使用
ref
,仅更新/维护已有内容时才使用
chore

Step 4: Generate and Propose

步骤4:生成并提议分支名

Build the branch name as
<username>/<type>/<short-description>
.
Rules for
<short-description>
:
  • Kebab-case, lowercase
  • 3 to 6 words, concise but clear
  • Describe the change, not file names
  • Only use ASCII letters, digits, and hyphens — no spaces, dots, colons, tildes, or other git-forbidden characters
Present it to the user and ask if they want to use it, modify it, or change the type.
分支名格式为
<username>/<type>/<short-description>
<short-description>
的命名规则:
  • 小写kebab-case格式
  • 3到6个单词,简洁清晰
  • 描述改动内容,而非文件名
  • 仅使用ASCII字母、数字和连字符 —— 不允许空格、点、冒号、波浪线或其他git禁止的字符
将生成的分支名展示给用户,询问是否要使用、修改或者调整分支类型。

Examples

示例

Work descriptionBranch name
Dropdown menu not closing on outside click
priscila/fix/dropdown-not-closing-on-blur
Adding search to conversations page
priscila/feat/add-search-to-conversations
Restructuring drawer components
priscila/ref/simplify-drawer-components
Updating test fixtures
priscila/chore/update-test-fixtures
Bumping @sentry/react to latest version
priscila/chore/bump-sentry-react
Adding a new agent skill
priscila/feat/add-create-branch-skill
工作描述分支名
下拉菜单点击外部区域未关闭
priscila/fix/dropdown-not-closing-on-blur
为会话页面新增搜索功能
priscila/feat/add-search-to-conversations
重构抽屉组件
priscila/ref/simplify-drawer-components
更新测试夹具
priscila/chore/update-test-fixtures
升级@sentry/react到最新版本
priscila/chore/bump-sentry-react
新增Agent skill
priscila/feat/add-create-branch-skill

Step 5: Create the Branch

步骤5:创建分支

Once confirmed, detect the current and default branch:
bash
git branch --show-current
git remote | grep -qx origin && echo origin || git remote | head -1
git symbolic-ref refs/remotes/<remote>/HEAD 2>/dev/null | sed 's|refs/remotes/<remote>/||' | tr -d '[:space:]'
If
symbolic-ref
fails, fall back to
git branch --list main master
: use the one that exists; if both or neither exist, ask the user.
If
git branch --show-current
is empty (detached HEAD), show the current commit (
git rev-parse --short HEAD
) and ask whether to branch from it or switch to the default branch first.
Otherwise, if the current branch is not the default branch, warn the user and ask whether to branch from the current branch or switch to the default branch first.
If the user wants to switch to the default branch, handle any uncommitted changes appropriately (offer to stash them if present), then run
git checkout <default-branch>
. On any failure, restore stashed changes if applicable and stop.
Before creating the branch, check that the name doesn't already exist locally or on the remote (
git show-ref
). If it does, ask the user to choose a different name.
Create the branch:
bash
git checkout -b <branch-name>
Restore any stashed changes after the branch is created.
分支名确认后,检测当前分支和默认分支:
bash
git branch --show-current
git remote | grep -qx origin && echo origin || git remote | head -1
git symbolic-ref refs/remotes/<remote>/HEAD 2>/dev/null | sed 's|refs/remotes/<remote>/||' | tr -d '[:space:]'
如果
symbolic-ref
执行失败,回退执行
git branch --list main master
:使用存在的分支;如果两个分支都存在或者都不存在,询问用户。
如果
git branch --show-current
返回为空(分离头指针状态),展示当前提交(
git rev-parse --short HEAD
),询问是从该提交切分支还是先切换到默认分支。
否则,如果当前分支不是默认分支,向用户发出警告,询问是从当前分支切分支还是先切换到默认分支。
如果用户选择切换到默认分支,妥善处理所有未提交的变更(如果存在则提议stash暂存),然后运行
git checkout <default-branch>
。如果任何步骤执行失败,恢复已暂存的变更(如果有)并终止操作。
创建分支前,检查分支名在本地和远端是否已经存在(
git show-ref
)。如果已存在,要求用户选择其他名称。
创建分支:
bash
git checkout -b <branch-name>
分支创建完成后恢复所有暂存的变更。

References

参考