Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readGit

Review a PR without stashing your work

Use git worktree add to check out a second branch in its own folder, review it, and remove it, while your edits stay put.

You are halfway through feature/search and the code does not compile yet. A teammate asks you to review feature/login-form. Switching branches means stashing or committing work you are not ready to commit, and untangling it afterwards.

A second working folder avoids both. Git calls it a worktree, and you create one with a single command.

Add a worktree

A linked worktree is a second folder checked out from the same repository. It has its own files, index and HEAD. Both worktrees share the object database and most refs, including local branches.

$ git fetch origin
$ git worktree add ../app-review feature/login-form
Preparing worktree (new branch 'feature/login-form')
branch 'feature/login-form' set up to track 'origin/feature/login-form'.
HEAD is now at bdb1452 add login form

I put the new folder next to the repository, which is what ../app-review means. You have no local feature/login-form, but origin does, so git creates one that tracks the remote branch. The command does not touch your original folder, which still has feature/search with its uncommitted edits.

diagram

One repository, two folders

One repository, two folders: each worktree has its own branch and files while both share the same history.

Review, then remove it

Open ../app-review in your editor and read the change. The folder is a fresh checkout. Local dependencies such as an ignored node_modules directory stay in the original folder. Run your install command there before you run the tests.

Your editor on the first folder keeps its open files and unsaved edits while you read.

When you are done, go back to your own folder, list the worktrees and remove the review one:

$ git worktree list
/work/app         999244a [feature/search]
/work/app-review  bdb1452 [feature/login-form]
$ git worktree remove ../app-review

The list shows each folder with its commit and branch. It is also how I find review folders I forgot to remove. After the remove, app-review is gone and git status in your folder still shows your unfinished edits.

Branch and cleanup limits

By default, Git refuses to check out the same branch in two worktrees. While the review worktree exists, adding another one for the same branch fails:

$ git worktree add ../again feature/login-form
Preparing worktree (checking out 'feature/login-form')
fatal: 'feature/login-form' is already used by worktree at '/work/app-review'

git worktree remove also refuses when the folder has modified or untracked files. Save changes you want to keep before removing the folder. The local review branch remains after git worktree remove.