Skip to content

Instantly share code, notes, and snippets.

@tnm
Created September 22, 2025 22:57
Show Gist options
  • Select an option

  • Save tnm/920d909edae68d970af8aec052ea34cf to your computer and use it in GitHub Desktop.

Select an option

Save tnm/920d909edae68d970af8aec052ea34cf to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<EOF
Usage: gwc <branch> [file]
Ensures a git worktree exists for <branch>, creates it if needed,
and opens Claude Code in that worktree (optionally opening [file]).
Options:
-h, --help Show this help message
EOF
}
main() {
if [[ $# -lt 1 ]]; then
usage
exit 1
fi
case "$1" in
-h|--help)
usage
exit 0
;;
esac
branch="$1"
file="${2:-}"
dir="../$branch"
if [[ -d "$dir" ]]; then
echo "✔ Worktree $dir already exists, reusing..."
else
echo "ℹ Checking for branch $branch ..."
if git show-ref --verify --quiet "refs/heads/$branch"; then
echo "✔ Found local branch $branch"
git worktree add "$dir" "$branch"
elif git ls-remote --exit-code origin "refs/heads/$branch" >/dev/null 2>&1; then
echo "✔ Found remote branch $branch"
git worktree add "$dir" "$branch"
else
echo "⚠ Branch $branch not found, creating new branch"
git worktree add -b "$branch" "$dir"
fi
fi
echo "🚀 Opening Claude Code in $dir"
cd "$dir"
if [[ -n "$file" ]]; then
claude --ide "$file"
else
claude --ide
fi
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment