Search
Git Worktree
Git worktree is an addition that comes automatically with git and can help you switching branches without the need of git stash and git checkout branch etc.
Instead, you can inside a cloned (it works best with a bare-repo git clone --bare https://superlink) git repo create a branch such as:
git worktree add master master or create a detatched one from master: git worktree add branchB
Git worktree will create a new subfolder called branchB (cd branchB), with changing directory into it, you are in that branch. By cd out of it (cd ..) you are back in master. If you create more with the above add branch, you can switch between them by simply switching directories! All changes won’t need to stash as they are staying inside the folder and that branch.
# Setup: Bare Clone vs Regular Clone
Normal git clone:
|
|
A git worktree with bare (recommended for worktree workflow) looks like this:
|
|
# General Checkout
|
|
# General Fetch/Pull/Push
|
|
# Create a New Worktree Based on an Existing Remote Branch
Probably I want a PR instead, which is explained above or in Checking out PR (and branch) from existing PR
To instead work on an existing branch in a new worktree, use git worktree add <path> <branch>.
e.g. to work on chris’s hackday branch I used:
git worktree add hack-days-erd chris/hack-normalization-erd- or for working on docs branch
git worktree add simon-docs-databricks-update simon-docs-databricks-update
# Push the Local Branch to origin/remote
Checkout as above with -b branch-name. And publish with git push -u origin branch-name
# Checking out PR
See Checking out PR (and branch) from existing PR.
# Advanced Feature
ThePrimeagen even created a plugin for it to easily switch between branches with telescope:
- GitHub - ThePrimeagen/git-worktree.nvim
- telesope repos check at: YouTube at t=280
# Merge Master into Current Feature-branch
git - How to merge branch from another worktree - Stack Overflow
# There is also a Vim Extension
GitHub - ThePrimeagen/git-worktree.nvim made by theprimagen
# git fetch And Workaround
git fetch - not working with git worktree
# git pull/push Does not Work either
git worktree - how to handle pulling or fetching
# AI Workloads
Worktree’s are a great way to work with AI Agents/Vibe Code Agents. There’s also already a couple of AI mini orchestrators, that spawn out multiple agents or orchestrate them.
As well as tmux, where each session can have multiple sessions, which all can run multiple agents.