With Git 2.42 (Q3 2023), 'git worktree add
'(man) learned how to create a worktree based on an orphaned branch with --orphan
.
So this should be supported.
See commit 926c40d, commit 128e549, commit 35f0383, commit 7ab8918, commit 9ccdace, commit ed6db0e, commit 1b28fbd, commit b71f919 (17 May 2023) by Jacob Abel (0xnu11pwn
).
(Merged by Junio C Hamano -- gitster
-- in commit 4dd0469, 22 Jun 2023)
worktree add
: introduce "try --orphan
" hint
Signed-off-by: Jacob Abel
Add a new advice/hint in git worktree add
(man) for when the user tries to create a new worktree from a reference that doesn't exist.
Current Behavior:
% git init foo
Initialized empty Git repository in /path/to/foo/
% touch file
% git -C foo commit -q -a -m "test commit"
% git -C foo switch --orphan norefbranch
% git -C foo worktree add newbranch/
Preparing worktree (new branch 'newbranch')
fatal: invalid reference: HEAD
%
New Behavior:
% git init --bare foo
Initialized empty Git repository in /path/to/foo/
% touch file
% git -C foo commit -q -a -m "test commit"
% git -C foo switch --orphan norefbranch
% git -C foo worktree add newbranch/
Preparing worktree (new branch 'newbranch')
hint: If you meant to create a worktree containing a new orphan branch
hint: (branch with no commits) for this repository, you can do so
hint: using the --orphan option:
hint:
hint: git worktree add --orphan newbranch/
hint:
hint: Disable this message with "git config advice.worktreeAddOrphan false"
fatal: invalid reference: HEAD
% git -C foo worktree add -b newbranch2 new_wt/
Preparing worktree (new branch 'newbranch')
hint: If you meant to create a worktree containing a new orphan branch
hint: (branch with no commits) for this repository, you can do so
hint: using the --orphan option:
hint:
hint: git worktree add --orphan -b newbranch2 new_wt/
hint:
hint: Disable this message with "git config advice.worktreeAddOrphan false"
fatal: invalid reference: HEAD
%
git config
now includes in its man page:
worktreeAddOrphan
Advice shown when a user tries to create a worktree from an
invalid reference, to instruct how to create a new orphan
branch instead.
d=subdir; n=some-branch; git worktree add --detach --no-checkout $d; git -C $d checkout --orphan $n; git -C $d reset; git -C $d clean -fdq; echo hello >$d/test.txt; git -C $d add test.txt; git -C $d commit -m init
– Groundling