What I do
- Detect the repository default branch from
origin/HEAD, then fall back to local main, master, or develop
- Review the current session context plus staged and unstaged git changes to infer the branch purpose
- Generate a short branch name in
<type>/<brief-description> format
- Create and switch to the new branch while preserving local changes when safe
- Ask the user instead of guessing when the intent or safe branching base is ambiguous
When to use me
Use this skill when the user asks to create a new git branch for the current work and wants the branch name derived from the session context and current git changes.
Prerequisites
git must be installed
- Use
git switch when available; if the installed Git is too old to support it, use git checkout / git checkout -b instead
- The repository should have a detectable default branch from
origin/HEAD or local main, master, or develop
- If the current branch is not the detected default branch and the worktree is dirty, ask the user before switching branches
Default Branch Detection
Check the current branch:
git branch --show-current
Detect the remote default branch first:
default_branch="$(git symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null)"
default_branch="${default_branch#origin/}"
If the remote default branch is unavailable, fall back in this order:
for candidate in main master develop; do
if git show-ref --verify --quiet "refs/heads/$candidate"; then
default_branch="$candidate"
break
fi
done
If no default branch can be determined, stop and ask the user which branch should be used as the base.
Change Analysis
- Review the current agent session context and the user's latest request.
- Inspect the current git-visible work:
git status --short
git diff --cached --stat
git diff --stat
- Prefer the user request and session goal over raw filenames when naming the branch.
- Use changed files and diff summaries as supporting context to infer the branch type and slug.
- If there are no local changes yet, derive the branch name from the session goal alone.
Branch Name Generation
- Format:
<type>/<brief-description>
- Types:
feat/ for new capabilities or additions
fix/ for bugs, regressions, or broken behavior
docs/ for documentation-only work
test/ for test-only work
refactor/ for structural changes without intended behavior changes
chore/ for maintenance, tooling, config, or fallback cases
- Slug rules:
- Keep it lowercase and hyphenated
- Prefer 3-5 words when possible
- Use the session goal first, with changed files as supporting evidence
- Avoid vague slugs like
updates or misc-fixes
- Examples:
feat/add-git-branch-skill
fix/handle-default-branch-detect
docs/update-skill-readme
If the intent is still unclear after reviewing the session and git changes, ask the user for the branch name instead of guessing.
If the generated local branch name already exists, append a short numeric suffix until it is unique:
git show-ref --verify --quiet "refs/heads/<branch-name>"
Branch Creation Workflow
Detect the current branch and the default branch.
Generate a descriptive branch name from the session context and current git changes.
If already on the detected default branch, create the new branch directly from the current state:
git switch -c <branch-name>
# Fallback for older Git versions:
git checkout -b <branch-name>
This preserves staged and unstaged changes on the new branch.
If on a different branch and the worktree is clean, return to the default branch first, then create the new branch:
git switch <default-branch>
# Fallback for older Git versions:
git checkout <default-branch>
git switch -c <branch-name>
# Fallback for older Git versions:
git checkout -b <branch-name>
If on a different branch and the worktree is dirty:
- Do not stash, reset, or discard changes automatically
- Ask the user whether to branch from the current state or first return to
<default-branch>
After creating the branch, confirm the result:
git branch --show-current
git status --short
Error Handling
- Could not detect default branch → "Error: Could not determine the default branch from
origin/HEAD or local main/master/develop."
- Branch intent still unclear → ask the user for the desired branch name
- Dirty worktree on a non-default branch → ask before switching branches
- Branch switch/create command fails due to uncommitted changes, conflicts, or unsupported git subcommands → surface the git error and stop
- Generated branch name already exists locally → add a numeric suffix like
-2 or -3
- Never use destructive commands like
git reset --hard and never discard the user's changes automatically
1---2name: git-branch3description: Create a descriptive feature branch from the repo default branch using session context and current git changes4license: MIT5---67## What I do89- Detect the repository default branch from `origin/HEAD`, then fall back to local `main`, `master`, or `develop`10- Review the current session context plus staged and unstaged git changes to infer the branch purpose11- Generate a short branch name in `<type>/<brief-description>` format12- Create and switch to the new branch while preserving local changes when safe13- Ask the user instead of guessing when the intent or safe branching base is ambiguous1415## When to use me1617Use this skill when the user asks to create a new git branch for the current work and wants the branch name derived from the session context and current git changes.1819## Prerequisites2021- `git` must be installed22- Use `git switch` when available; if the installed Git is too old to support it, use `git checkout` / `git checkout -b` instead23- The repository should have a detectable default branch from `origin/HEAD` or local `main`, `master`, or `develop`24- If the current branch is not the detected default branch and the worktree is dirty, ask the user before switching branches2526## Default Branch Detection27281. Check the current branch:29 ```bash30 git branch --show-current31 ```32332. Detect the remote default branch first:34 ```bash35 default_branch="$(git symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null)"36 default_branch="${default_branch#origin/}"37 ```38393. If the remote default branch is unavailable, fall back in this order:40 ```bash41 for candidate in main master develop; do42 if git show-ref --verify --quiet "refs/heads/$candidate"; then43 default_branch="$candidate"44 break45 fi46 done47 ```48494. If no default branch can be determined, stop and ask the user which branch should be used as the base.5051## Change Analysis52531. Review the current agent session context and the user's latest request.542. Inspect the current git-visible work:55 ```bash56 git status --short57 git diff --cached --stat58 git diff --stat59 ```603. Prefer the user request and session goal over raw filenames when naming the branch.614. Use changed files and diff summaries as supporting context to infer the branch type and slug.625. If there are no local changes yet, derive the branch name from the session goal alone.6364## Branch Name Generation6566- **Format**: `<type>/<brief-description>`67- **Types**:68 - `feat/` for new capabilities or additions69 - `fix/` for bugs, regressions, or broken behavior70 - `docs/` for documentation-only work71 - `test/` for test-only work72 - `refactor/` for structural changes without intended behavior changes73 - `chore/` for maintenance, tooling, config, or fallback cases74- **Slug rules**:75 - Keep it lowercase and hyphenated76 - Prefer 3-5 words when possible77 - Use the session goal first, with changed files as supporting evidence78 - Avoid vague slugs like `updates` or `misc-fixes`79- **Examples**:80 - `feat/add-git-branch-skill`81 - `fix/handle-default-branch-detect`82 - `docs/update-skill-readme`8384If the intent is still unclear after reviewing the session and git changes, ask the user for the branch name instead of guessing.8586If the generated local branch name already exists, append a short numeric suffix until it is unique:87```bash88git show-ref --verify --quiet "refs/heads/<branch-name>"89```9091## Branch Creation Workflow92931. Detect the current branch and the default branch.942. Generate a descriptive branch name from the session context and current git changes.953. If already on the detected default branch, create the new branch directly from the current state:96 ```bash97 git switch -c <branch-name>98 # Fallback for older Git versions:99 git checkout -b <branch-name>100 ```101 This preserves staged and unstaged changes on the new branch.1021034. If on a different branch and the worktree is clean, return to the default branch first, then create the new branch:104 ```bash105 git switch <default-branch>106 # Fallback for older Git versions:107 git checkout <default-branch>108109 git switch -c <branch-name>110 # Fallback for older Git versions:111 git checkout -b <branch-name>112 ```1131145. If on a different branch and the worktree is dirty:115 - Do not stash, reset, or discard changes automatically116 - Ask the user whether to branch from the current state or first return to `<default-branch>`1171186. After creating the branch, confirm the result:119 ```bash120 git branch --show-current121 git status --short122 ```123124## Error Handling125126- Could not detect default branch → "Error: Could not determine the default branch from `origin/HEAD` or local `main`/`master`/`develop`."127- Branch intent still unclear → ask the user for the desired branch name128- Dirty worktree on a non-default branch → ask before switching branches129- Branch switch/create command fails due to uncommitted changes, conflicts, or unsupported git subcommands → surface the git error and stop130- Generated branch name already exists locally → add a numeric suffix like `-2` or `-3`131- Never use destructive commands like `git reset --hard` and never discard the user's changes automatically