gitmacos

Does running git switch --orphan lib switch me to the lib branch?


If I understand correctly running the following should create an empty orphaned lib branch.

% git switch --orphan lib
Switched to a new branch 'lib'

And git seems to indicate that it has now switched to the lib branch.

However if I run:

% git branch
  master

It seems to indicate that I'm on master still.

How do I know which branch I'm on?

Also if I try to switch to the lib branch, this is the message.

git checkout lib
error: pathspec 'lib' did not match any file(s) known to git

So it seems the lib branch was never created ...

Update

Someone says it works in one of the answers, so I updated git to the latest version on MacOS.

git version 2.39.3 (Apple Git-145)

And I've repeated the entire experiment starting from scratch and these are all the steps.

 % mkdir dir
 % cd dir
 % touch file
 % cd ..
 % git add *
 % git commit -m "First Commit"
[main (root-commit) 3060034] First Commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 dir/file
% git switch --orphan lib
Switched to a new branch 'lib'
 % git branch
  main

Answer Comment

OK got it now. We have to actually do a commit on the new branch in order for git branch to show that we are on the branch...

So this is what I did.

 % touch file
 % git add * && git commit -m "Lib Commit"
[lib (root-commit) de7b684] Lib Commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 dir/file
 % git branch
* lib
  main


Solution

  • Things work as intended. In particular, if you ask for the status

    $ git status
    On branch lib
    
    No commits yet
    
    nothing to commit (create/copy files and use "git add" to track)
    

    you see that you are about to start with a clean slate, most importantly, there are no commits, yet.