Skip to main content

Make the most of AI Agents using Git Worktrees

· 2 min read

Claude Code Git Worktrees

Most probably, you are using a tool like Claude Code to help you solve bugs, build features, and ask questions. There is a lot of work to do and many Jira tickets waiting to be resolved.

Common Scenario

Imagine the following. You work on a feature and after a long Claude Code session your feature is done and ready to be reviewed.

You open a pull request and you wait for the other software engineers to review it.

Since you have opened your pull request, you decide to move on to the next Jira ticket. You have made some progress, and an engineer on your team has asked you to review their pull request. At the same time, your pull request has some feedback, and changes are required at this point.

So you have 3 git branches active and 3 Claude Code sessions running so you need to work on all of them at the same time. You run a bunch of git stash, git checkout, and git rebase commands. A git hell. Sounds familiar?

The solution

This is what git worktrees came to solve. Instead of constantly switching between branches with git checkout, Git worktrees let you have separate directories for each branch. You can have multiple terminal emulators open, each one in a different branch, and run multiple Claude Code sessions simultaneously.

This is an example tree structure of a repository. Each directory contains the whole codebase checked out in the specific branch.

$ tree

project_name
└── worktrees
├── main_branch
│ └── .git
├── feature_1424
├── bug_322
└── code_review_branch

When you want to create a new branch to develop a feature you run these commands:

$ cd project_name

$ git -C worktrees/main_branch worktree add ../feature_1425 -b feature_1425

$ tree

project_name
└── worktrees
├── main_branch
│ └── .git
├── feature_1424
├── bug_322
├── code_review_branch
└── feature_1425

$ cd worktrees/feature_1425

$ claude

Having to work at 3 issues at the same time requires a lot of context switching. You need to be focused in order to deliver successfully. You should not have to deal with git stashes.

Git worktrees help me to be productive and I hope you find them useful.