Can I create a worktree for an empty Git repo?
Asked Answered
G

2

6

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.

Gerkman answered 9/8, 2021 at 14:6 Comment(5)
What would you expect that worktree to contain? There are no tree objects in your repository yet, so there's nothing to export.Semilunar
I would expect a worktree to be blank, i.e. to have the same state as the main worktree has just after git init.Gerkman
I don't think there's a way, because there's nothing (no commit-id, no tree-id, not tag, no branch) that this worktree could conceivably track. You could always create an empty commit to bootstrap your repository with and then start branching from there.Semilunar
Exactly. Just make a placeholder commit. git worktree add somepath $(git commit-tree -m placeholder `git mktree <&-`) and now you've got an empty worktree you can fill in later.Mental
BTW, I'd generally suggest </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
T
8

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).

Taam answered 9/8, 2021 at 18:55 Comment(3)
This patchset (Nov. 2022) seems related.Philander
I'm proud to announce that despite taking quite a bit of time, the aforementioned patchset has finally graduated to master (commit 4dd0469328) and should be in the next git release (2.42). With this patchset, there is now a --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
This is [now implemented](https://mcmap.net/q/1599441/-checking-out-orphan-branch-in-new-work-tree, with Git 2.42 (Q3 2023).Philander
B
0
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.

Bunch answered 20/4, 2023 at 20:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.