Search

Search IconIcon to open search

Git Worktree

Last updatedUpdated: by Simon Späti · CreatedCreated:

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
git clone https://github.com/user/repo.git
cd repo
# Structure:
#   repo/
#   ├── .git/           # repository data
#   ├── src/            # working files (main branch checked out)
#   └── ...
#
# The repo root IS your main branch. Worktrees become "secondary":
git worktree add ../feature-branch feature-branch

A git worktree with bare (recommended for worktree workflow) looks like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
git clone --bare https://github.com/user/repo.git repo.git
cd repo.git
# Structure:
#   repo.git/
#   ├── HEAD
#   ├── config
#   ├── objects/
#   └── ...             # just git data, NO working files
#
# All branches are equal—each lives in its own worktree:
git worktree add main main
git worktree add feature-branch feature-branch

# Final structure:
#   repo.git/           # bare repo (never edit directly)
#   main/               # main branch worktree
#   feature-branch/     # feature branch worktree

# General Checkout

1
2
3
4
5
6
7
8
# Create a new directory with the specified branch checked out into it:
git worktree add path/to/directory existing-branch-name

#Create a new directory with a new branch checked out into it:
git worktree add path/to/directory -b new_branch

#same as above but defining the branch it's based on:
git worktree add ../app-example-2 origin/main -b bug-fix-new-branch

# General Fetch/Pull/Push

1
2
3
4
5
6
7
# git fetch/pull/push does not work as bare repos have no remote history, therefore we need to specify

git fetch origin branch-name
git pull origin branch-name

# HEAD refers to the current commit you're on (in the detached HEAD state).
git push origin HEAD:branch-name 

more below on and

# 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:

# 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.

# Git Worktree Alternatives

# Futher Reads


Origin: Git’s Best And Most Unknown Feature - YouTube