I have an empty Git repository (i.e. it contains no commits yet). Is there a way to create a worktree using git worktree add
command (with --no-checkout
option) for this repo? I mean to instruct Git not to associate a newly created worktree with any HEAD
yet.
In a completely empty repository, there can be no branches, because each branch name must identify one actual, existing commit.1 The main worktree, however, must be on some branch. So Git has it on a non-existent branch, which Git calls both an unborn branch (in various messages and internal code) and an orphan branch (in flags like git checkout --orphan
).
In theory, git worktree add
could also allow --orphan
, to put a new work-tree on an unborn branch. It does not currently do so, however. Should you wish to write new code for Git that allows it to do this, and send it to the Git maintenance folks, it might be a candidate for inclusion in a future Git release. It's probably a good idea to include some sort of reasonably compelling use case.
Until then, you can't do this: you must make at least one initial commit (which then allows an infinite number of branch names to be created).
Edit: it seems --orphan
might be in the next Git release (2.40, probably). See VonC's comment.
1This constraint is somewhat arbitrary, but Git makes it anyway, so one must live with it (or rewrite Git).
--orphan
flag that you can use however for an empty repository it should just "do what you mean" when using the base git worktree add
command. –
Cockspur git init
git checkout -b empty-commit-dont-touch
git commit --allow-empty --message=Init
git worktree add --detach ../empty-worktree
Now you have that empty-commit-dont-touch
branch but at least the two
working directories are empty. That’s the best I could do.
© 2022 - 2024 — McMap. All rights reserved.
tree
objects in your repository yet, so there's nothing to export. – Semilunargit init
. – Gerkmangit worktree add somepath $(git commit-tree -m placeholder `git mktree <&-`)
and now you've got an empty worktree you can fill in later. – Mental</dev/null
over<&-
; a nonexistent file descriptor often triggers outright errors where one pointing to a device (like/dev/null
) that immediately EOFs is generally handled more gracefully. – Woodsum