Does one git worktree support multiple branches?

0

Issue

Per the documentation, each git worktree serves one branch (dev/feature/prod and so on), but it also sound not reasonable to me, that each branch will have its own worktree as it will creates many folders, and at some point might be confusing.

Does one git worktree can support multiple branches? for example all the branches which belongs to feature and then switch between what relevant for the moment? Is it correct approach?

Solution

From https://git-scm.com/docs/git-worktree

A git repository can support multiple working trees, allowing you to check out more than one branch at a time. With git worktree add a new working tree is associated with the repository. This new working tree is called a "linked working tree" as opposed to the "main working tree" prepared by git-init or git-clone.

If you want to add a new branches to worktree you need to write:
git worktree add <path> <branch_name>

For example:

git checkout -B new_branch
git checkout main
git worktree add ./new_branch new_branch

To remove it:
git worktree remove

And that how does it look:
in tree view

In path parameter you can pass anything. So you can manage your branches as you want for example you can create folder where you will store only branches with bugfixes or futures.

Answered By – Filip Poplewski

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More