Skip to Content
Navigation:

A stick figure smiling

If you want to see more comics like this, sign up for my saturday comics newsletter or browse more comics!

Image of a comic. To read the full HTML alt text, click "read the transcript".

browse more comics! get the zine!
read the transcript!

git worktree lets you have 2 branches checked out at the same time

Illustration of a smiling stick figure with curly hair, and a git worktree, represented by a box with a smiley face

person: ugh, I want to take a look at this other branch, but I have all these uncommitted changes…
git worktree: i can help!

creating a worktree

You can check out a branch into a new directory like this:

git worktree add ~/my/repo mybranch

(my is the directory, mybranch is the branch)

Then you can run any normal git commands in the new directory:

$ cd ~/my/repo
$ git pull

two worktrees cant have the same branch checked out

Here’s what happens if you try:

$ git checkout main
fatal: main is already checked out at /home/bork/work/homepage

it’s way faster (and uses less space!) than cloning the repository again

Because worktrees share a .git directory, it just needs to check out the files from the branch you want to use!

other worktree commands

List all worktrees:

$ git worktree list

Delete a worktree:

$ git worktree remove ~/my/repo

sometimes I use worktrees to keep my .git directory and its checkout separate

this lets me put the checkout in Dropbox but not the .git directory:

$ git clone --bare  git@github.com:jvns/myrepo
$ cd myrepo.git
$ git worktree add ~/Dropbox/myrepo main

(Dropbox is the directory, main is the branch)

Saturday Morning Comics!

Want another comic like this in your email every Saturday? Sign up here!

I'll send you one of my favourite comics from my archives every Saturday.
© Julia Evans 2024 | All rights reserved (see the FAQ for notes about licensing)