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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s