Search code examples
gitgit-worktree

Difference between git main and linked working tree?


Is there any difference between a "main working tree" and a "linked working tree" in git? If so, what differences are there?


Git has a subcommand called git-worktree used to be able to manage multiple working trees in one repository. When cloned with this command:

git clone <url> --separate-git-dir=.git main

the directory main will contain the "main working tree". After that a new working tree can be created with git worktree add temp. temp is now a "linked working tree".

Is there any way to tell the difference of the two types? One thing should be that main can not be removed in the same way as temp (git worktree remove temp). But are there other things that are possible/impossible to do with one kind of the worktree and not the other?


Solution

  • Possible-vs-impossible: the answer should be no, in general. The only restriction on the main working tree is that you cannot remove it.

    Actual: yes, occasionally other bits show through. For instance, when acting as the receiver of a git push, at least in older versions of Git, only the main working tree's branch was considered. (This may still be the case; I have not tested it.)

    Git has to distinguish internally between "main" and "added" working tree because the traditional location of traditional files is directly in .git: e.g., .git/HEAD contains the main working tree's current branch and .git/index contains the main working tree's default index. Other added working trees have their HEAD and index files elsewhere. This is the source of those differences that "leak out" of Git. They are not intended to leak, but sometimes they do.