Created
September 3, 2025 10:03
-
-
Save christianalfoni/68739df88222d06184c0d6cac98440d1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
w() { | |
local branch="$1" | |
if [[ -z "$branch" ]]; then | |
echo "Usage: w <branch-name>" | |
return 1 | |
fi | |
# must be in a git repo | |
git rev-parse --is-inside-work-tree >/dev/null 2>&1 || { | |
echo "Not in a git repository." | |
return 1 | |
} | |
# figure out paths: new worktree sits next to the current folder | |
local parent_dir | |
parent_dir="$(cd .. && pwd)" | |
local target="$parent_dir/$branch" | |
if [[ -e "$target" ]]; then | |
echo "Target folder already exists: $target" | |
return 1 | |
fi | |
git fetch --all --prune | |
# detect default branch (main/master/whatever origin points to) | |
local default_branch | |
default_branch="$(git symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null | cut -d/ -f2)" | |
[[ -z "$default_branch" ]] && default_branch="main" | |
# If local branch exists -> use it | |
if git rev-parse --verify --quiet "$branch" >/dev/null; then | |
git worktree add "$target" "$branch" || return $? | |
# If remote branch exists -> check that out, then create local tracking branch | |
elif git show-ref --verify --quiet "refs/remotes/origin/$branch"; then | |
git worktree add "$target" "origin/$branch" || return $? | |
(cd "$target" && git switch -c "$branch" --track "origin/$branch") | |
# Otherwise create a brand-new branch from default | |
else | |
git worktree add -b "$branch" "$target" "origin/$default_branch" || return $? | |
fi | |
cd "$target" || return $? | |
# -- Ensure upstream is configured (single, robust path) -------------------- | |
# If no upstream is set, 'git push -u' will either create the remote branch | |
# and set tracking, or just set tracking if it already exists. | |
if ! git rev-parse --abbrev-ref --symbolic-full-name "@{u}" >/dev/null 2>&1; then | |
git push -u origin "$branch" || return $? | |
echo "Upstream configured: $branch -> origin/$branch" | |
fi | |
# -------------------------------------------------------------------------- | |
# launch Claude if available | |
if command -v c >/dev/null 2>&1; then | |
c | |
else | |
echo "Tip: 'c' command not found on PATH. Start Claude manually if needed." | |
fi | |
} | |
wrm() { | |
local branch="$1" | |
if [[ -z "$branch" ]]; then | |
echo "Usage: wrm <branch-name>" | |
return 1 | |
fi | |
local parent_dir | |
parent_dir="$(cd .. && pwd)" | |
local target="$parent_dir/$branch" | |
# remove worktree (folder must not be in use) | |
git worktree remove "$target" 2>/dev/null || git worktree remove --force "$target" | |
# delete local branch if it exists (and not checked out elsewhere) | |
if git rev-parse --verify --quiet "$branch" >/dev/null; then | |
git branch -d "$branch" 2>/dev/null || git branch -D "$branch" | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment