Sitemap
Nathan Hedglin

Nathan Hedglin

Multitask Like a Pro with Worktrees

2 min readMay 12, 2024

--

Git worktree is a native git feature that allows managing multiple working trees within a git repository. No more ugly WIP commits or stashing to switch branches. A worktree is a new working tree (current changes on a branch) stored in a new directory. Before I demonstrate HOW to use git worktrees, let’s discuss the WHY I use them over other git features.

Press enter or click to view image in full size

The Pain of Multitasking

There are several instances where simply doing a git add . && git commit -m "WIP”or git stash will work poorly or not at all.

For example, let’s say I’m currently compiling or running a test suite locally and want to review my coworker’s PR changes locally. I’d normally have to wait for compilation or testing to finish (for large projects that can take a while), commit or stash any changes, switch branches, let the IDE reindex the whole project / workspace, rebuild the whole project for my coworker’s branch, do my stuff, switch back to my branch, let the IDE reindex the whole project / workspace again, either soft reset the WIP commit or pop the stash, rebuild the code, reopen the files I was in and continue — or simply git worktree add <worktree> <branch>for my coworker’s branch, open that directory in another IDE instance, do my stuff, close it, and continue.

With worktree, I leave my current changes and tests running while essentially checking out my coworker’s branch in a different directory. I can completely leave by current code and IDE alone. I can create a new instance of my IDE or use different IDE entirely; such as VS Code for frontend and IntelliJ for backend.

Worktrees are cheaper and easier than recloning the repo to isolate a branch in a new directory. Commits on a worktree are the same as committing to the branch they’re based on!

Get Nathan Hedglin’s stories in your inbox

Join Medium for free to get updates from this writer.

Worktrees are show with git branch but have a + before their name.

> git branch
main develop *feature/foo +main-worktree
(END)

Here are a few use cases for git worktree

  • checkout changes while compiling or running tests
  • comparing two versions simultaneously
  • efficiently switch between branches without having to recompile, IDE reindex the code, reopen files etc.

Create a Worktree

Let’s say I have a project in the hello-world directory. I will create a dedicated directory for worktrees outside of my project directory. Don’t worry, git can track worktrees outside it’s own directory. I’ll create a worktree for the branch bug/fix-bar that I want to code review.

git worktree add .worktrees/hello-world/bug/fix-bar bug/fix-bar

Now I can cd into .worktrees/hello-world/bug/fix-bar or open it with another IDE. When I’m done, I’llcd back to the project directory or go back to the other IDE instance; I’ll find ALL of my uncommitted changes patiently waiting for me.

--

--