Skip to content

Instantly share code, notes, and snippets.

@Aaronontheweb
Last active August 10, 2025 23:28
Show Gist options
  • Save Aaronontheweb/a2144aae970faa50388bbb4aa122d1a5 to your computer and use it in GitHub Desktop.
Save Aaronontheweb/a2144aae970faa50388bbb4aa122d1a5 to your computer and use it in GitHub Desktop.
Git Worktree Bash Helpers
cwt() {
local new_branch="$1"
local base_branch="${2:-dev}"
if [[ -z "$new_branch" ]]; then
echo "usage: cwt <new-branch> [base-branch=dev]"
return 2
fi
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
echo "Error: not inside a git repository."
return 2
fi
local repo_root repo_name target_dir
repo_root="$(git rev-parse --show-toplevel)" || return 2
repo_name="$(basename "$repo_root")"
# Centralized worktree directory (default ~/.worktrees)
local base_dir="${WORKTREE_DIR:-$HOME/.worktrees}"
target_dir="${base_dir}/${repo_name}/${new_branch}"
if [[ -e "$target_dir" ]]; then
echo "Error: target directory already exists: $target_dir"
return 2
fi
mkdir -p "$(dirname "$target_dir")"
git fetch --prune origin
if git show-ref --verify --quiet "refs/heads/${new_branch}"; then
echo "Branch '${new_branch}' exists locally. Creating worktree from it..."
git worktree add "$target_dir" "$new_branch" || return 2
else
local base_ref
if git show-ref --verify --quiet "refs/heads/${base_branch}"; then
base_ref="$base_branch"
elif git show-ref --verify --quiet "refs/remotes/origin/${base_branch}"; then
base_ref="origin/${base_branch}"
else
echo "Error: base branch '${base_branch}' not found locally or on origin."
return 2
fi
echo "Creating new branch '${new_branch}' from '${base_ref}' in a new worktree..."
git worktree add -b "$new_branch" "$target_dir" "$base_ref" || return 2
fi
echo "✅ Worktree created at: $target_dir"
cd "$target_dir" || {
echo "Warning: could not change to directory $target_dir"
return 2
}
}
rwt() {
local branch="$1"
if [[ -z "$branch" ]]; then
echo "usage: rwt <branch>"
return 2
fi
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
echo "Error: not inside a git repository."
return 2
fi
local repo_root repo_name target_dir base_dir
repo_root="$(git rev-parse --show-toplevel)" || return 2
repo_name="$(basename "$repo_root")"
base_dir="${WORKTREE_DIR:-$HOME/.worktrees}"
target_dir="${base_dir}/${repo_name}/${branch}"
# Step 1: Remove worktree
git worktree remove "$target_dir" --force 2>/dev/null || {
echo "Warning: git worktree remove failed or not found, removing directory manually."
rm -rf "$target_dir"
}
# Step 2: Delete branch (local only)
if git show-ref --verify --quiet "refs/heads/${branch}"; then
git branch -D "$branch"
echo "Deleted local branch: $branch"
fi
echo "✅ Worktree cleanup complete for branch '$branch'."
}
@Aaronontheweb
Copy link
Author

To enable, after adding cwt and rwt to your .bashrc

export WORKTREE_DIR="$HOME/.worktrees" # optional, default is ~/.worktrees
source ~/.bashrc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment