Worktrees enable checking out multiple branches out at the same time.
You could just clone the whole repo to a new folder to check out a different branch.
π MyApp/
π MyApp-hotfix/
π MyApp-featureX/
But with worktrees you can just use the existing repo to generate more folders with states of different branches.
π MyApp/
βββ π .git
βββ π main
βββ π hotfix
βββ π featureX
Why
In mobile, quickly switching branches for bugfix/review doesnβt really work. With heavily compiled projects a branch switch often causes a long recompilation. Its more efficient to have multiple variants ready on disk.
With AI, having multiple variants allows parallelizing the work agents and you are doing.
Why worktrees
Youβll have a single shared .git folder:
- Less wasted storage.
- You can see the local branches across the folders.
- Remote updates are fetched together - Itβs still a single git repo on disk.
Setup:
-
Create a root folder for the project.
mkdir MyApp cd MyApp -
Clone β.git folderβ of the repo
git clone --bare --config remote.origin.fetch="+refs/heads/*:refs/remotes/origin/*" git@gh.com:u/repo.git .gitclone --bare: will copy the repository, but will not populate the source files on disk. We want the sources to be populated in separate worktree folders later.--config remote.origin.fetch: Because bare is intended for git server, it omits the configuration of mapping remote branches toorigin/*. We add it back, so our client could see the branches in remote..git: We name this folder .git. This is convenient as this is the standard .git folder, and now any tooling will also be able to identify it as such. Meaning you can execute git commands in the root MyApp folder.
Repo state now:
π MyApp/ βββ .git/ <-- Repo history βββ config βββ HEAD βββ objects/ ... -
Add a worktree
git worktree add mainsyntax is:
git worktree add <worktree_path> -b <branch_name>The first command will create a worktree folder in path
mainand will try to checkout or create a branch namedmain.Repo now:
π MyApp/ βββ .git/ <-- Repo history βββ main/ <-- Project source files (Worktree) βββ .git <-- Points to shared .git dir at root. βββ app/ βββ build.gradle βββ src/ -
Profit
Open the worktree folder
mainin IDE to work on it. Add more worktrees if needed. -
Managing worktrees
List them by:
$ git worktree list /Users/me/MyApp (bare) /Users/me/MyApp/main 7ac0703be [main] /Users/me/MyApp/feature aa45c02a1 [feature/fix-123]Delete:
git worktree remove <worktree_name>Note: Renaming/deleting the worktree folders manually needs caution, as the shared .git is refering to those folders.
Extra
Why not just open an existing repo and worktree add straight away?
You can, but then that worktree would appear alongside the existing source files. So its messy and you have to .gitignore it. Structure:
π MyApp/ <-- Existing repo and worktree
βββ .git/
βββ src/ <-- Existing source files
βββ build.gradle. <-- Existing source files
βββ MyApp-Hotfix/ <-- New worktree with new source files
βββ ...
Worktrees in practise
It is more beneficial to have persistent worktrees named by the kind of work you are doing. Such as: work, review, side-work. This means that you are just switching branches inside the worktree, as opposed to creating new worktree/folder for each branch.
With this you can just use your IDE to switch branches. And since you are not opening new folders, you donβt have to do the full initial setup that comes with new projects.