“detached HEAD”消息是警告,而不是错误。
原因很简单。 Git 对于分支有两种状态:
当您在分支上进行新提交时,分支会自动前进以包含新提交。
When you are not on a branch and make new commits, the non-branch also advances, but—here's the reason for the warning—if you then switch to some other branch (so that you are on it), git forgets where you were.1 When you are on a branch, the branch name remembers the new commits; when you are not, there is nothing to remember them.
您不能位于远程跟踪分支上
分支origin/branch2
is a 远程跟踪分支:即一个记住“在哪里”的分支branch2
was on origin
上次我们(我们的 git 和origin
的 git)进行了一次关于分支的对话”。因为这样做的目的是跟踪分支的位置they是,git 不会让你“进入”该分支并进行新的提交(然后它会记住在哪里)you是而不是在哪里they were).
因为你不能在上面,所以检查它会给你“分离的头”状态。
但您可以在普通(本地)分支上
分支branch2
是一个正常的、普通的、当地的分支机构。这是你的,你可以随心所欲地处理。您可以继续并进行新的提交。
(您的本地分支还可以记住远程跟踪分支,因为它是所谓的upstream。 Git 对此的令人困惑的术语是,您的本地分支正在“跟踪”远程跟踪分支。 “跟踪”这个词在这里出现了太多次,“分支”这个词也是如此,都有不同的含义。)
1Actually it saves it for a while, in the reflog for HEAD
, but this is only good for 30 days by default.