Checking out orphan branch in new work-tree
Asked Answered
T

3

6

I know that a new orphan branch can be created like so:

git checkout --orphan <new_branch>

however this uses my current work tree which I want to leave completely untouched. I tried:

git --work-tree=/tmp/test checkout --orphan <new_branch>

but this also seem to use my current work tree and not the one I specified using --work-tree.

I could use a second clone, but that does not seem optimal. Any way of solving this using work-trees?

Tailing answered 26/10, 2018 at 9:40 Comment(0)
M
14

Make a worktree with detached head then orphan it:

git worktree add --detach /.../dir
cd /.../dir
git checkout --orphan branch
Mandamus answered 25/12, 2018 at 6:3 Comment(1)
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 initGroundling
A
3

You can try git-worktree.

git checkout --orphan <new_branch>
git commit
git worktree add /tmp/test <new_branch>

# switch to the previous branch
git checkout -
# or
git checkout <previous_branch>

cd /tmp/test
# do something to <new_branch>

Now /tmp/test is a sub worktree. It shares the same .git with the main worktree. If you don't need the sub worktree any more, you can simply remove /tmp/test. The new commits are stored in the main repository.

If your Git does not support git-worktree yet, you need a newer version.

Arctogaea answered 26/10, 2018 at 11:5 Comment(1)
I actually ended up with something similar here now when investigating. Just that I created the worktree first, and then did the orphan checkout there. Then I avoid switching branch in the main work tree.Tailing
K
2

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.

Kairouan answered 25/6, 2023 at 21:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.