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

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

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


当前回答

当你签出到一个commit git checkout <commit-hash>或一个远程分支时,你的HEAD将被分离,并尝试在上面创建一个新的提交。

任何分支或标记都无法访问的提交将在30天后被垃圾收集并从存储库中删除。

解决这个问题的另一种方法是为新创建的提交和签出创建一个新分支。Git checkout -b <branch-name> <commit-hash>

本文将说明如何进入分离HEAD状态。

其他回答

我刚才无意中重复了这句话:

列出远程分支 Git branch -r /功能/ f1234起源 来源/主 我想在本地签出一个,所以我剪切粘贴: git checkout origin/Feature/f1234 您看!分离HEAD状态 你处于“分离的头部”状态。[…])


解决方案1:

不包括起源/在我的分支规格前检查时:

git checkout Feature/f1234

解决方案2:

添加-b参数,用于从远程创建本地分支

git checkout -b origin/Feature/f1234或

git checkout -b Feature/f1234它会自动回到原点

一个简单的偶然的方法是做一个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.

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

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

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

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

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

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

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