Igor Djordjevic <igor.d.djordje...@gmail.com> writes: >> 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,
phillord@fuel:~/scratch/git-test$ git init Initialised empty Git repository in /home/phillord/scratch/git-test/.git/ phillord@fuel:~/scratch/git-test$ touch README phillord@fuel:~/scratch/git-test$ git add README phillord@fuel:~/scratch/git-test$ git commit [master (root-commit) 86b7232] Initial Commit 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 README phillord@fuel:~/scratch/git-test$ ls README phillord@fuel:~/scratch/git-test$ git checkout --orphan temp Switched to a new branch 'temp' phillord@fuel:~/scratch/git-test$ ls README phillord@fuel:~/scratch/git-test$ git status On branch temp Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: README I would have expected README to disappear since it's not on this branch. > 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 > <https://git-scm.com/docs/git-checkout#git-checkout---orphanltnewbranchgt> > for more info). I guess it is this that is confusing me, yes. > 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] > Ah, that's interesting. You're creating the worktree to a unnamed branch, then switching branches to the one you wanted. I guess this would also be a good way of implementing git worktree add --orphan ../my-new-branch which would seem to be what I really want. Thanks for the help! Phil -- 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 git-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.