Magnus' blog


Parallel development work with worktrees

Recently, I have been exploring different ways AI can benefit my workflow. However, I find it incredibly annoying to sit and watch the AI go. Therefore, I started using worktrees for the AI to work in. With the current models I have access to at work, there is still a lot of work that requires my involvement. The worktree approach has turned out to work well for both AI and hand-coding, especially when waiting for slow tasks like rebuilding, downloading dependencies, or starting the application.

A worktree is set up quite easily with the git command:

git worktree add ../worktree1

While worktrees can be removed and re-added later, I just keep four permanent worktrees that I work in continuously. Four seems to be about the right number for me: I can either work alongside a few AI agents myself, or have AI agents running in all four while I work on other tasks like answering emails and planning.

This has expanded even more recently: not only do I use worktrees, but I have also modified the applications we run so they can run on different ports and use different databases. This is quite resource-intensive, so I would like to share a few ways I made it more efficient.

For the database, in order to save space and compute, everything is done in a single PostgreSQL container with a “central” database that is patched to the latest development version. All development then happens in a “worktree1”, “worktree2”, … database. To get started, I made a command for copying the “central” database to the worktree’s database. Using create database 'worktreeX' with template 'central', this is actually a lot faster than the previous approach. It also makes testing patches easier, as a patch can easily be tested, discarded, and tried again.

For the applications, the number in the worktree folder name is simply added to the port number. For example, for a directory called worktree1, 1 is added to the port.

It took around a day to set up and a few more days to adapt various workflows to use the new ports.

It has been a major life-changer at work, as multiple tasks can be run at once. Sometimes I have the AI do stuff; other times I can just work on three different tasks myself without waiting for all the slow tasks.

View next or previous post: