Skip to content

Instantly share code, notes, and snippets.

@caffeinum
Last active June 12, 2025 01:48
Show Gist options
  • Save caffeinum/6d201a00043a54727e91a2958bb8eec2 to your computer and use it in GitHub Desktop.
Save caffeinum/6d201a00043a54727e91a2958bb8eec2 to your computer and use it in GitHub Desktop.
Put into `~/.local/bin/feature`. Export `ANTHROPIC_API_KEY` in your `.bashrc`

feature

Usage

Run once in a repo to setup worktree directory:

feature init

or manually:

mkdir .worktrees
echo ".worktrees/" >> .gitignore
git commit -a -m "update .gitignore"

Export ANTHROPIC_API_KEY (you may put it in ~/.bashrc)

export ANTHROPIC_API_KEY="sk-ant-follow-me-on-twitter-@caffeinum"

Then, to run multiple Claudes, run:

feature "refactor /app directory"

It will generate an appropriate git branch name, create a git worktree in a dedicated .worktrees directory, and then hand off to Claude to implement the feature, commit the changes, and open a pull request, all with a single command

Ref

※ Use git worktrees to run multiple Claude sessions in parallel. Learn more (​https://docs.anthropic.com/s/claude-code-worktrees​)

#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<EOF
usage: feature [--help] [init] "task description"
creates a git worktree branch using claude for naming, then cds into it and asks claude to implement, commit & open a pr.
commands:
init update .gitignore and create worktrees dir
options:
--help show this help message and exit
EOF
}
if [[ "${1-}" == "--help" ]]; then
usage
exit 0
fi
if [[ "${1-}" == "init" ]]; then
mkdir -p .worktrees
grep -qxF ".worktrees/" .gitignore || echo ".worktrees/" >> .gitignore
echo "Initialized feature workflow. Please commit this updated .gitignore to your repository."
exit 0
fi
if [[ $# -lt 1 ]]; then
echo "error: missing task description" >&2
usage >&2
exit 1
fi
desc="$*"
# detect original user’s home (if sudo’d)
if [[ -n "${SUDO_USER-}" ]]; then
real_home=$(getent passwd "$SUDO_USER" | cut -d: -f6)
export HOME="$real_home"
fi
# ask claude for exactly the branch name
prompt="decide if this is a chore/feature/refactor/etc and suggest a git branch name for: \"$desc\". reply with EXACTLY the branch name and nothing else."
raw=$(claude -p "$prompt" 2>&1) || raw="$raw"
branch=$(echo "$raw" | head -n1 | tr -d '\r')
# detect invalid api key
if echo "$branch" | grep -qi "invalid api key"; then
echo "error: invalid api key, please run 'claude login' and try again" >&2
exit 1
fi
# ensure .worktrees dir exists and is ignored
mkdir -p .worktrees
grep -qxF ".worktrees/" .gitignore || echo ".worktrees/" >> .gitignore
# create worktree + branch
git worktree add ".worktrees/$branch" -b "$branch" >/dev/null
cd ".worktrees/$branch"
which claude
claude --version
# hand off to claude to code, commit & pr
claude "task: \"$desc\". when done, create a commit and open a pull request on current branch"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment