(Note: This is NOT about creating a worktree with a detached HEAD.)
A typical worktree added with git worktree add
(whether created with --detached
or otherwise) has its state maintained in the main repository, including its HEAD
and index
. The main repository "knows" about it when you do git worktree list
.
So the question here is: how do you create a worktree that the main repository doesn't "know" about? The worktree should maintain its HEAD
and index
inside itself, but refer to the parent repository for referencing commits and the like.
CodePudding user response:
Turns out this is actually very simple. The only thing you need is a directory containing the following:
.git/HEAD
, containing a commit hash (terminated with a newline).git/commondir
, containing a relative path to the main.git
directory from this file itself (terminated by a newline)
You can now use git reset
, git checkout
, etc. inside this directory, and it will refer to the original repository for the commit graph and all needed objects, but which the original repository won't know about.