好。这让我很困惑。我刚刚初始化了一个运行命令git init
的空git存储库。然后我将一个文件添加到git的对象库,即git add .
我试图创建一个分支,即git branch {branchname}
。我收到一个错误,因为根据我所读到的内容,我无法创建一个分支而没有至少提交过一次。好的。我会咬人的。然后我尝试git checkout -b {branchname}
。它似乎有效,即我被运送到我刚创建的分支机构。哇?我尝试git checkout master
并收到错误error: pathspec 'master' did not match any file(s) known to git.
为什么我能够创建新分支而不是结帐master branch
?我忽略了什么或者没有理解?
答案 0 :(得分:1)
master
或origin
一样,{p> upstream
是一项惯例,而非要求。由于您在master
中没有提交,因此您也没有任何提交。 HEAD
指向尚不存在的引用。当你运行git checkout -b branch
时,你会问git:
HEAD
指向的引用复制到。HEAD
指向新参考。由于您的HEAD
指向不存在的引用,因此您不会复制任何内容,最终仍然指向任何内容。只要您继续切换分支而不提交,情况就是这样。
一旦你做了第一次提交,一切都会改变。现在你已经提交了一个提交,你有一个参考,你当前的HEAD
指向。下次创建分支时,会有一个重复的引用,因此下次切换时分支不会消失。
实施例(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.
答案 1 :(得分:0)
它最初对我不起作用......
durrantm.../play$ mkdir ggg
durrantm.../play$ cd ggg
durrantm.../ggg$ git init
Initialized empty Git repository in /home/durrantm/play/ggg/.git/
durrantm.../ggg$ git checkout -b newy
fatal: You are on a branch yet to be born
......直到是的,我对master做了初步提交:
durrantm.../ggg$ vi x.x
durrantm.../ggg$ git add .
durrantm.../ggg$ git commit -m"Initial Commit"
[master (root-commit) 75bcf19] Initial Commit
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 x.x
durrantm.../ggg$ git checkout -b abc
Switched to a new branch 'abc'
durrantm.../ggg$