Skip to content

Instantly share code, notes, and snippets.

@josephbolus
Forked from dhh/agent-git-trees.sh
Last active January 19, 2026 06:08
Show Gist options
  • Select an option

  • Save josephbolus/be942516c4f4dea08ee27ee9bce7f07c to your computer and use it in GitHub Desktop.

Select an option

Save josephbolus/be942516c4f4dea08ee27ee9bce7f07c to your computer and use it in GitHub Desktop.
# Create a new worktree and branch from within current git directory.
ga() {
if [[ -z "$1" ]]; then
echo "Usage: ga [branch name]"
exit 1
fi
local branch="$1"
local base="$(basename "$PWD")"
local worktree_path="../${base}--${branch}"
git worktree add -b "$branch" "$worktree_path"
mise trust "$worktree_path"
cd "$worktree_path"
}
# Remove worktree and branch from within active worktree directory.
gd() {
if gum confirm "Remove worktree and branch?"; then
local cwd base branch root
cwd="$(pwd)"
worktree="$(basename "$cwd")"
# split on first `--`
root="${worktree%%--*}"
branch="${worktree#*--}"
# Protect against accidentially nuking a non-worktree directory
if [[ "$root" != "$worktree" ]]; then
cd "../$root"
git worktree remove "$worktree" --force
git branch -D "$branch"
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment