Skip to content
Latchkey

git worktree Command: Parallel Checkouts in CI

git worktree attaches additional working trees to a single repository, each on its own ref.

A matrix CI job that needs two branches at once (for example to diff or build both) can use worktrees to avoid a second clone, sharing object storage between trees.

Common flags

  • add <path> <ref> - create a new working tree at path checked out to ref
  • add --detach <path> <commit> - add a detached worktree at a commit
  • list - show all working trees and their HEADs
  • remove <path> - remove a worktree
  • prune - clean up administrative records of deleted worktrees
  • --force - override safety checks when adding or removing

Example

shell
# Build current branch and main side by side without recloning
git worktree add ../main-tree origin/main
( cd ../main-tree && make build )
git worktree remove ../main-tree

In CI

Worktrees share the object database, so a second worktree is far cheaper than a second clone when a job needs two refs at once. Remember to git worktree remove (or prune) at the end so cached repos do not accumulate stale trees.

Key takeaways

  • git worktree add gives a second checkout that shares objects, avoiding a reclone.
  • It is ideal for jobs that must build or diff two refs simultaneously.
  • Clean up with worktree remove or prune on reused runners.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →