Git Worktree: Work on Two Git Branches at the Same Time

Switching across branches can sometimes become annoying, especially if you’re currently working on different fixes or features or similar features that require you to keep continually changing branches for reference.

Of course, you could clone the repo somewhere else and maintain these two copies, but there’s also an integrated approach available since Git 2.5+: git-worktree.

This command allows you to checkout different branches on different working trees attached to the main repository.

Add a new worktree

git worktree add ../new-worktree-dir existing-branch

This command will create a new worktree on the specified directory checking out the instructed branch.

Remove a worktree

git worktree remove ../new-worktree-dir

When you finish with a worktree, you can remove it with this command. If you delete the worktree using the filesystem without using the remove option, it’s associated administrative files will eventually be removed automatically.

Leave a Reply