Create Branch
Announce: "I'm using the branch skill to create a well-named feature branch."
Step 1: Gather Context
Run in parallel:
- •
git status --short(check for uncommitted changes) - •
git branch --show-current(current branch) - •
git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null(detect default branch)
If this is not a git repository: inform the user and stop.
Step 2: Parse Input
Extract from $ARGUMENTS:
- •Ticket ID: Look for patterns like
[A-Z]+-[0-9]+(e.g.,PROJ-1234,JIRA-567). - •Description: The remaining text after removing the ticket ID, or the entire argument if no ticket ID is found.
If no arguments are provided:
code
AskUserQuestion( header: "Branch name", question: "What should the branch be for? Provide a ticket ID and/or short description.", options: [] )
Step 3: Build Branch Name
Determine the branch prefix from git config user.name (lowercase, first token, e.g., "Rodrigo Fernandes" becomes rodrigo). Fall back to feature if git config is not set.
Construct the branch name using this pattern:
- •If a ticket ID is present:
<prefix>/<slug>-<TICKET-ID> - •If no ticket ID:
<prefix>/<slug>
Where:
- •
<prefix>is the user prefix derived above - •
<slug>is the description converted to lowercase, with spaces replaced by hyphens, non-alphanumeric characters removed, and truncated to 50 characters.
Examples (assuming prefix rodrigo):
- •
add user authentication PROJ-1234->rodrigo/add-user-authentication-PROJ-1234 - •
fix login timeout bug->rodrigo/fix-login-timeout-bug - •
refactor database layer CORE-99->rodrigo/refactor-database-layer-CORE-99
Step 4: Create the Branch
Determine the base branch:
- •
git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null— extract branch name (e.g.origin/main→main). - •If that fails:
git remote set-head origin --auto 2>/dev/nulland retry step 1. - •If still unresolved: fall back to
mainiforigin/mainexists, thenmasteriforigin/masterexists. - •If nothing works: ask the user.
If there are uncommitted changes: warn the user but proceed (the changes will carry over to the new branch).
Run:
- •
git fetch origin <base>(ensure base is up to date) - •
git checkout -b <branch-name> origin/<base>
Report the created branch name and base to the user:
code
Created branch: <branch-name> (from origin/<base>)
Error Handling
- •Not a git repository: inform the user and stop.
- •Branch already exists: report the conflict. Offer to switch to the existing branch or suggest an alternative name.
- •Fetch failure: report the error. Suggest checking network connectivity or remote configuration.
- •Default branch not detected: follow the Default Branch Detection procedure in Step 4, then ask the user if all fallbacks fail.
- •No description provided: prompt for one before proceeding.