DEV Community

Stephen McCullough
Stephen McCullough

Posted on • Originally published at swm.cc

Git Worktree for Multiple Branches

Just learned about git worktree and it's genuinely useful.

Problem: Need to work on feature branch whilst also reviewing a PR or checking main. Usually means stashing changes and switching branches.

Solution: Multiple working directories for the same repo.

# Create a new worktree for a branch
git worktree add ../myrepo-feature feature-branch

# Now you have:
# ~/code/myrepo       (main branch)
# ~/code/myrepo-feature (feature branch)
Enter fullscreen mode Exit fullscreen mode

Can have both open in different editors, run different dev servers, etc. Much cleaner than branch switching or having multiple clones.

Clean up when done:

git worktree remove ../myrepo-feature
Enter fullscreen mode Exit fullscreen mode

List all worktrees:

git worktree list
Enter fullscreen mode Exit fullscreen mode

Simple but effective.

Top comments (0)