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?
Make a worktree with detached head then orphan it:
git worktree add --detach /.../dir
cd /.../dir
git checkout --orphan branch
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With