Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking out orphan branch in new work-tree

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?

like image 527
Zitrax Avatar asked Oct 26 '18 09:10

Zitrax


2 Answers

Make a worktree with detached head then orphan it:

git worktree add --detach /.../dir
cd /.../dir
git checkout --orphan branch
like image 193
max630 Avatar answered Oct 22 '22 09:10

max630


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.

like image 24
ElpieKay Avatar answered Oct 22 '22 09:10

ElpieKay