Skip to content

Instantly share code, notes, and snippets.

@programaker
Last active March 6, 2026 15:30
Show Gist options
  • Select an option

  • Save programaker/3484b8d75655249b32c3deff7568be9a to your computer and use it in GitHub Desktop.

Select an option

Save programaker/3484b8d75655249b32c3deff7568be9a to your computer and use it in GitHub Desktop.
Git worktree workflow

Git worktree workflow

Start a project

Clone or create a new project using gitwt.

It uses a "bare repository" strategy. This way, there's a clear root for the project and there's no need to clone everything - new worktrees only duplicate code, git database stuff remains in a single place.

Work on a task (new branch)

From the root of the project:

git fetch origin
git worktree add -b <branch-name> <branch-dir> [<base-branch>] # `main` by default
cd <branch-dir>

(work commit push review merge)

git worktree remove <branch-name>

NOTE: remove can fail to delete the worktree dir if an editor is still open. In this case, the branch is deleted, but the physical folder remains

Checking out existing branch

From the root of the project:

git fetch origin
git worktree add <branch-dir> <branch-name>
cd <branch-dir>

(work commit push review merge)

git worktree remove <branch-name>

Git tries to resolve it in this order:

  1. Local branch exists → checks it out in the new worktree
  2. Local branch doesn't exist, but a matching remote-tracking branch does → automatically creates a local branch tracking the remote one (same as git checkout feature-branch when origin/feature-branch exists)
  3. Neither exists → error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment