我今天结束了一个分离的头,同样的问题描述:git推送说所有最新的,即使我有局部的变化

据我所知,我没有做任何不同寻常的事情,只是从本地回购中提交和推送。

那么,我是如何得到一个分离的头部的呢?


当前回答

进入git分离头状态的另一种方法是尝试提交到远程分支。喜欢的东西:

git fetch
git checkout origin/foo
vi bar
git commit -a -m 'changed bar'

请注意,如果您这样做,任何进一步尝试签出origin/foo将使您回到分离的头部状态!

解决方案是创建自己的本地foo分支来跟踪origin/foo,然后可选地push。

这可能与您最初的问题无关,但该页在“git分离头”的谷歌次点击中排名靠前,这个场景的文档记录严重不足。

其他回答

如果您有一个与分支同名的标记,就会发生这种情况。

例如:如果“release/0.1”是标签名,那么

git checkout release/0.1

在“release/0.1”处产生分离的HEAD。如果您期望release/0.1是一个分支名称,那么您会感到困惑。

如果您试图通过重新签出文件而撤消所做的更改,并且语法不完全正确,则很容易发生这种情况。

你可以看看git日志的输出——你可以把上次成功提交后的日志尾部粘贴到这里,我们都可以看到你做了什么。或者你可以粘贴它,然后在freenode IRC上的#git中询问。

如果不能使用git开关,使用'git checkout -b [branch]'而不是'git checkout [branch]'

一个简单的偶然的方法是做一个git签出头作为head的拼写错误。

试试这个:

git init
touch Readme.md
git add Readme.md
git commit
git checkout head

这给了

Note: checking out 'head'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 9354043... Readme

分离HEAD意味着当前签出的不是本地分支。

导致分离HEAD状态的一些场景:

If you checkout a remote branch, say origin/master. This is a read-only branch. Thus, when creating a commit from origin/master it will be free-floating, i.e. not connected to any branch. If you checkout a specific tag or commit. When doing a new commit from here, it will again be free-floating, i.e. not connected to any branch. Note that when a branch is checked out, new commits always gets automatically placed at the tip. When you want to go back and checkout a specific commit or tag to start working from there, you could create a new branch originating from that commit and switch to it by git checkout -b new_branch_name. This will prevent the Detached HEAD state as you now have a branch checked out and not a commit.