Skip to content

Instantly share code, notes, and snippets.

@dhirschfeld
Created December 1, 2025 01:55
Show Gist options
  • Select an option

  • Save dhirschfeld/464c3c0223e4c32643293ecb6f8a9018 to your computer and use it in GitHub Desktop.

Select an option

Save dhirschfeld/464c3c0223e4c32643293ecb6f8a9018 to your computer and use it in GitHub Desktop.
Git Worktrees
git config --global alias.wt-new '!f() { \
url="$1"; \
repo_name=$(basename "$url" .git); \
echo "Creating bare worktree repository: $repo_name"; \
mkdir "$repo_name" && \
cd "$repo_name" && \
git clone --bare "$url" .git && \
git config --add remote.upstream.fetch "+refs/heads/*:refs/remotes/upstream/*" && \
git fetch upstream && \
echo "Creating master worktree... "; \
git worktree add master master && \
cd master && \
git branch --set-upstream-to=upstream/master && \
echo "✓ Successfully created bare worktree repository: $repo_name"; \
echo " Repository: $repo_name/. git"; \
echo " Master worktree: $repo_name/master"; \
}; f'
git config --global alias.wt-delete '! f() { \
echo "Removing worktree: $1"; \
git worktree remove "$1" && \
echo "Deleting branch: $1"; \
git branch -D "$1" && \
echo "✓ Successfully deleted worktree and branch: $1"; \
}; f'
git config --global alias.wt-add '!f() { \
source="${2:-upstream/master}"; \
case "$source" in \
*/*) \
remote="${source%/*}"; \
echo "Fetching from $remote..."; \
git fetch "$remote" || { echo "Failed to fetch from $remote"; return 1; }; \
;; \
esac; \
git_dir="$(git rev-parse --git-common-dir)"; \
parent_dir="$(cd "$git_dir/.." && pwd)"; \
worktree_path="$parent_dir/$1"; \
echo "Creating worktree: $1 from $source"; \
git worktree add --no-track -b "$1" "$worktree_path" "$source" && \
echo "✓ Successfully created worktree: $1 at $worktree_path"; \
}; f'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment