Hi Philip,

On Friday, December 8, 2017 at 7:43:45 PM UTC+1, Phillip Lord wrote:
> 
> I'm trying to create a new worktree, but for an orphan branch. But I 
> can't find a way of doing this convieniently. 
> 
> I can checkout a new orphan branch: 
> 
> git checkout --orphan my-new-branch 
> 
> That works, but now I can't make a worktree because, I presume there 
> are no commits yet. 
> 
> fatal: invalid reference: another-new-branch

Indeed, worktree should point to something inside repository, and 
currently you have nothing - even your "my-new-branch" doesn`t really 
exist yet (as being orphaned it has no history), unless you commit 
something to it, as you noticed.

Doing `git branch` confirms this, no "my-new-branch" listed.

> More over, it leaves any files on master in place. 

Hmm, not sure what you remark to here, but if it is about leaving 
index and working tree in the same state as where you left off (I 
guess "master", in your case), that is by design, and it can be 
easily cleaned-up with `git rm -rf .`, if desired (see `git-checkout  
<https://git-scm.com/docs/git-checkout#git-checkout---orphanltnewbranchgt>
--orphan` docs[1] 
<https://git-scm.com/docs/git-checkout#git-checkout---orphanltnewbranchgt> 
for more info).

> ... But, okay, I think, I can move back to master.
> 
> git checkout master 
> git worktree add ../my-new-branch my-new-branch 
> 
> But this fails for the same reason. 
> 
> git worktree  add ../my-new-branch my-new-branch 
> fatal: invalid reference: my-new-branch 

As your orphaned branch never really started existing in the first 
place (no commits on it), once you moved back to "master", you lost 
any track of it, and Git still (rightfully) complains about it 
missing just the same.

> So, the only solution is 
> 
> git checkout --orphan my-new-branch 
> 
> git commit (something) 
> 
> git checkout master 
> git worktree add ../my-new-branch my-new-branch 
> 
> All of which seems pretty painful. 
> 
> Am I missing something? 

On top of everything said above, I may suggest a possibly more 
satisfying alternative:

    git worktree add --detach ../my-new-branch

We can inspect the current worktree state:

    $ git worktree list
    /test-repo                   273bf25 [master]
    /my-new-branch               273bf25 (detached HEAD)

Now, you can go to that worktree and do this:

    git checkout --orphan my-new-branch

Inspecting the situation again, we get this:

    $ git worktree list
    /test-repo                   273bf25 [master]
    /my-new-branch               0000000 [my-new-branch]


Do note that "my-new-branch" still doesn`t really exist until the 
first commit is made there (as explained at the beginning of this 
e-mail), but might be you find this flow a bit more convenient...?

Regards, Buga

[1] https://git-scm.com/docs/git-checkout#git-checkout---orphanltnewbranchgt

-- 
You received this message because you are subscribed to the Google Groups "Git 
for human beings" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to