Version Control

What are Git worktrees and how do you use them?

How Git worktrees let you check out different branches in separate folders of one repository, with git worktree add, list and remove, and the one-branch rule.

  • 6 min read
  • 5 questions
  • +35 XP
Transcript

Git Worktrees, explained by cats. Kitty has one Git repository. She wants to fix a bug on the main branch. But she also wants to build a new feature. Normally, Kitty changes branches back and forth. Main. Then feature. Then main again.

Each time, files change. Things reload. Kitty gets confused. So Kitty uses Git worktrees. A worktree gives Kitty another folder from the same repository. One folder can use the main branch. Another folder can use feature-login branch. At the same time.

Same repository. Same history. Different folders. Kitty can even open each worktree in a different editor window. No Chaos. But there is one important rule: the same branch can only be open in one worktree at a time. So what are Git worktrees for?

Working on two things at once. Testing a branch in its own folder. Keeping work clean and separate. That’s Git worktrees. One repository. Multiple folders. Different branches. Much easier for Kitty.