git HEAD 在原点/主站分离
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25232056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HEAD detached at origin/master
提问by Snowcrash
I've just checked out an old project to fix a bug. git reports:
我刚刚检查了一个旧项目来修复一个错误。git 报告:
HEAD detached at origin/master
git status
reports I have an untracked file:
git status
报告我有一个未跟踪的文件:
<project name>.xcworkspace/xcshareddata/
I'd like to get on and fix the bug but I'm not sure quite what's going on. If I try git checkout master
I get:
我想继续并修复错误,但我不确定发生了什么。如果我尝试git checkout master
我得到:
error: The following untracked working tree files would be overwritten by checkout:
<project name>.xcworkspace/xcshareddata/
Can I just delete this file? Am I on the master
branch? If not, how do I get to it?
我可以删除这个文件吗?我在master
分行吗?如果没有,我该如何解决?
回答by torek
As CommuSoft says, you're not on master. You're in "detached HEAD" mode. You get that any time you explicitly check out something that is not a (local) branch name:
正如CommuSoft 所说,你不是在掌握。您处于“分离头”模式。每当您明确检查不是(本地)分支名称的内容时,您都会得到:
$ git checkout origin/master # detach to remote branch
or if there's a tag v1.7
:
或者如果有标签v1.7
:
$ git checkout v1.7 # detach to tag
and you can even explicitly detach when using a local branch name:
您甚至可以在使用本地分支名称时显式分离:
$ git checkout --detach master # forcibly detach
A "detached HEAD" means you're not on a branch. Being "on a branch" means you're not using the detached HEAD mode. Yes, that's pretty circular; see this questionand its answers for more details.
“分离的 HEAD”意味着您不在分支上。“在分支上”意味着您没有使用分离的 HEAD 模式。是的,这是非常循环的;有关更多详细信息,请参阅此问题及其答案。
As for the:
至于:
error: The following untracked working tree files would be overwritten ...
error: The following untracked working tree files would be overwritten ...
When you get git checkout
to move from one commit to another, it does two main things:
当你git checkout
从一个提交转移到另一个提交时,它主要做两件事:
- choose whether to be in "detached HEAD" mode, and
- rearrange the work tree to match the moved-to commit.
- 选择是否处于“分离头”模式,以及
- 重新排列工作树以匹配移动提交。
Step 2 is where the problem is occurring. You're on the commit identified by origin/master
, and in that commit, there is no record of the files git is currently complaining about. You've asked to switch to the commit identified by master
, which is obviously a different commit.1Git sees that in the commit identified by master
, there aresome (maybe just one) files with the same names, that are differentfrom the files or directories that are in your work-tree right now.
第 2 步是出现问题的地方。您origin/master
正在进行由 标识的提交,并且在该提交中,没有 git 当前抱怨的文件的记录。您已要求切换到由 标识的提交master
,这显然是不同的提交。1Git把,在提交的鉴定master
,也有一些(也许只有一个)文件具有相同的名称,是不同的,在你的工作树,现在的文件或目录。
In order to switch from the current commit to the new one, git checkout
must remove the existing files-or-directories and replace them with the ones in the new commit—the one you're asking to switch to. If those files or directories were tracked in the current commit, git would be happy to remove and replace them as needed, as git can always get them back for you: just switch back to that old commit, and there they are. But they're notin the current, to-be-switched-away-from, commit. So git tells you: "Hey, if I make this switch you asked for, I can't guarantee that I'll be able to restore these files and/or directories."
为了从当前提交切换到新提交,git checkout
必须删除现有的文件或目录,并将它们替换为新提交中的文件或目录 - 您要求切换到的那个。如果这些文件或目录在当前提交中被跟踪,git 会很乐意根据需要删除和替换它们,因为 git 总是可以为您取回它们:只需切换回旧提交,它们就在那里。但它们不在当前的、要切换的、提交中。所以 git 告诉你:“嘿,如果我按照你的要求进行这个切换,我不能保证我能够恢复这些文件和/或目录。”
It's now up to you, not git, to figure out what to do with these files and/or directories. Based on the error message, it's a directory,2and switching to master
will cause that directory to be removed and replaced with something else (possibly a different directory with some file(s) in it, possibly just a file). Do you:
现在由您而不是 git 来决定如何处理这些文件和/或目录。根据错误消息,它是一个目录,2并切换到master
将导致该目录被删除并替换为其他内容(可能是包含某些文件的不同目录,可能只是一个文件)。你:
- want to save it/them?
- if so, do you want to save it/them in a commit, or just move them out of the way?
- or do you just want to blow them away?
- 想保存它/他们吗?
- 如果是这样,您想将它/它们保存在提交中,还是只是将它们移开?
- 或者你只是想把它们吹走?
To save them, either commit them, or move them out of the way (e.g., rename them to a path that's not part of the work-tree, or to a different untracked name that's "safer", whatever that would be).
要保存它们,要么提交它们,要么将它们移开(例如,将它们重命名为不属于工作树的路径,或重命名为“更安全”的其他未跟踪名称,无论是什么)。
To simply blow them away, remove them manually or use git checkout -f
(force) to make git do it.
要简单地将它们吹走,请手动删除它们或使用git checkout -f
(force) 使 git 执行此操作。
Since you're not on a branch now (are in "detached HEAD" mode), if you want to commit them permanently to the repository, you can use something like the method CommuSoft added while I was writing this up. (You can create the new branch at any time, before or after doing a "git commit".)
由于您现在不在分支上(处于“分离的 HEAD”模式),如果您想将它们永久提交到存储库,您可以使用类似 CommuSoft 添加的方法。(您可以在执行“git commit”之前或之后随时创建新分支。)
You can also use git stash
. Git's stash
is deceptively complex little script: it makes commits that are not on any branch at all, that can later be transplanted to a branch. Using it is quite simple and easy: you just run git stash save
and all pending tracked changes are saved and cleaned up, or run git stash save -u
and all pending tracked changes anduntracked files are saved and cleaned up. They are now all safely squirreled away in the repository, under a commit, even though it's not a commit-on-a-branch.
您也可以使用git stash
. Gitstash
是一个看似复杂的小脚本:它使提交根本不在任何分支上,以后可以移植到分支上。使用它非常简单和容易:您只需运行git stash save
并保存和清理git stash save -u
所有挂起的跟踪更改,或者运行并保存和清理所有挂起的跟踪更改和未跟踪的文件。它们现在都安全地存放在存储库中,在提交下,即使它不是分支上的提交。
There is no single right answer for what to do here.
在这里做什么没有唯一的正确答案。
1Obviously different, because if you were already on the commit you're asking git to move to, then either the file would be in the commit and hence tracked, or it would be not-in-the-commit and hence you would not be asking git to clobber it.
1显然不同,因为如果你已经在提交你要求 git 移动到,那么文件要么在提交中,因此被跟踪,要么不在提交中,因此你不会要求 git 破坏它。
2This is a little odd. If I make a directory that will be clobbered by my git checkout
, and I make it as an emptydirectory, git just goes ahead and clobbers it. Here the difference between master^
and master
is that moving forward from master^
to master
creates file mxgroup.py
(so stepping back removes it):
2这有点奇怪。如果我创建一个将被 my 破坏的目录,并将git checkout
其作为一个空目录,git 就会继续并破坏它。这里的区别master^
,并master
为从前进master^
到master
创建文件mxgroup.py
(所以退一步删除它):
$ git checkout -q master^ # file goes away, now let's mkdir...
$ mkdir mxgroup.py; git checkout -q master
$ file mxgroup.py
mxgroup.py: Python script, ASCII text executable
However, if I have a non-empty directory, I get a different error message:
但是,如果我有一个非空目录,则会收到不同的错误消息:
$ git checkout -q master^ # file goes away; mkdir and make file
$ mkdir mxgroup.py; touch mxgroup.py/file; git checkout -q master
error: Updating the following directories would lose untracked files in it:
mxgroup.py
Aborting
But this is with git version 2.0.2; perhaps older gits are not as clever.
但这是 git 2.0.2 版;也许年长的 git 没有那么聪明。
回答by Willem Van Onsem
No you're not on the master branch, your on some kind of "newly invented" branch without a name that was once the master.
不,你不是在 master 分支上,而是在某种“新发明”的分支上,没有曾经是 master 的名字。
You simply need to switch back to the master:
您只需要切换回主服务器:
git checkout master
However if you wish to fix a bug, you should work at your own branch (according to most git workflows). Thus checkout the master and branch from it:
然而,如果你想修复一个 bug,你应该在你自己的分支上工作(根据大多数 git 工作流程)。因此检出 master 和分支:
git checkout master
git checkout -b fix-issue #give the branch a name that refers to the bug
then fix the bug and run:
然后修复错误并运行:
git checkout master
git merge --no-ff fix-issue -m "Fixed some strange issue, here follows a description"
EDIT
编辑
As specified in your question you have an untracked file. If these file(s) are of no importance, you can remove them. If on the other hand you wish to maintain them, you can merge them into the master first.
正如您的问题中所指定的,您有一个未跟踪的文件。如果这些文件不重要,您可以删除它们。另一方面,如果您希望维护它们,则可以先将它们合并到 master 中。
Therefore you create a temporary branch:
因此,您创建了一个临时分支:
git commit -m "some temporary message"
git checkout -b temporary
git checkout master
git merge --no-ff temporary