Skip to main content

Git tips & tricks

Git worktrees (working w/ multiple branches at once)

tip

Erwin Btw, quick Git trick to make working with multiple branches easier, without having to switch back and forth:

  • You can use Git worktrees to have multiple folders for different branches of the same repo. All worktrees use the same Git history on your computer, so creating one is fast (much faster than cloning).
  • Limitation: you can only have a single Git worktree per branch
  • My recommendation: keep a “main” folder for the main branch and moving between branches, create worktrees scoped to a specific branch you want to work on
    • For example, I have folders named monolith, monolith-tea-v5, monolith-cvd-plots etc… next to each other
  • Quick commands breakdown:
    • git worktree add path/to/folder branch-name creates a worktree in the folder, checking out an existing branch
    • git worktree add path/to/folder -b new-branch existing-branch creates a worktree in the folder by creating a new branch from an existing branch
    • git worktree remove path/to/folder removes the worktree and folder
  • Example: from the root of the repo, run git worktree add ../monolith-my-branch my-branch
  • This will create a new folder named monolith-my-branch next to your regular monolith folder, using the my-branch Git branch
  • Important: when the new folder is created, the Python environment is not set up, so you’ll have to run through the usual steps
    • uv sync -> source .venv/bin/activate -> mono sync
    • uv should blast through the install because everything is cached