master
, like origin
or upstream
, 是约定,不是要求。由于您在 中没有提交master
,因此您也没有任何参考。HEAD
指向一个尚不存在的 ref。当你运行时git checkout -b branch
,你要求 git:
HEAD
将您的点复制到 ref 。
- 将您
HEAD
指向新的参考。
由于您HEAD
指向不存在的 ref,因此您没有复制任何内容,最终仍然没有指向任何内容。只要你在不提交的情况下继续切换分支,就会出现这种情况。
一旦你做了你的第一次提交,一切都会改变。既然您已经做出了提交,那么您就有了一个可供当前HEAD
指向的参考。下次创建分支时,有一个要复制的 ref,因此下次切换时分支不会消失。
示例(ish):
git init
touch test
git add test
# There are no refs at this point, but HEAD is pointing to the nonexistent refs/heads/master
git checkout -b new_branch
git checkout master # fails, no refs (for master)
git checkout -b new_branch2
git checkout new_branch # fails, no refs (for new_branch)
git commit -m "Initial commit."
# Now you have a ref for new_branch2, but not yet for any other branches.
git checkout -b new_branch3 # new_branch2's ref is duplicated
git checkout new_branch2 # success, you made a commit in this branch so it has a ref
git checkout new_branch3 # success, you made a commit in new_branch2 which this branch's ref is also pointing to.
git checkout master # failure, until you run with '-b' master doesn't have any refs yet.