HEAD脱离原点/原版 [英] HEAD detached at origin/master

查看:115
本文介绍了HEAD脱离原点/原版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚签出了一个旧项目来修复错误. git报告:

I've just checked out an old project to fix a bug. git reports:

HEAD detached at origin/master

git status报告我有一个未跟踪的文件:

git status reports I have an untracked file:

<project name>.xcworkspace/xcshareddata/

我想继续并修复该错误,但我不确定发生了什么.如果我尝试git checkout master我会得到:

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:

error: The following untracked working tree files would be overwritten by checkout:
<project name>.xcworkspace/xcshareddata/

我可以删除此文件吗?我在master分支上吗?如果没有,我该怎么办?

Can I just delete this file? Am I on the master branch? If not, how do I get to it?

推荐答案

正如 CommuSoft所说的,您不在掌握.您处于分离式HEAD"模式.每当您显式签出不是(本地)分支名称的内容时,您都会得到:

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:

$ git checkout origin/master    # detach to remote branch

或是否有标签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

分离头"表示您不在分支上. 在分支上"意味着您没有使用分离的HEAD模式.是的,这很循环.有关更多详细信息,请参见此问题及其答案.

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 question and its answers for more details.

至于:

error: The following untracked working tree files would be overwritten ...

当您使git checkout从一个提交移到另一个提交时,它会执行两项主要操作:

When you get git checkout to move from one commit to another, it does two main things:

  1. 选择是否处于分离头"模式,并且
  2. 重新排列工作树以匹配移入的提交.

发生问题的地方是步骤2.您正在使用origin/master标识的提交,并且在该提交中,没有git当前正在抱怨的文件的记录.您已经要求切换到master标识的提交,这显然是一个不同的提交. 1 Git看到在master标识的提交中,有 are 某些(也许只是一个)同名文件,与您当前工作树中的文件或目录不同.

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.1 Git sees that in the commit identified by master, there are some (maybe just one) files with the same names, that are different from the files or directories that are in your work-tree right now.

为了从当前提交切换到新提交,git checkout必须删除现有的文件或目录,并用新提交(即您要切换到的提交)中的文件或目录替换它们.如果在当前提交中跟踪了这些文件或目录,则git很乐意根据需要删除并替换它们,因为git总是可以为您找回它们:只需切换回该旧提交,就可以了.但是在当前提交中,它们不是不是.所以git告诉您:嘿,如果我按您的要求进行此切换,则不能保证可以还原这些文件和/或目录."

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 not in 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."

现在由 you (而不是git)来决定如何处理这些文件和/或目录.根据错误消息,它是一个目录, 2 并切换到master会导致该目录被删除并替换为其他目录(可能是其中包含某些文件的其他目录,可能只是一个文件).你会吗?

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,2 and 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:

  • 要保存/保存吗?
  • 如果是的话,您是要保存它还是保存它们,或者只是将它们移开?
  • 还是只想把它们吹走?

要保存它们,请提交它们,或将它们移开(例如,将它们重命名为不属于工作树的路径,或重命名为更安全"的其他未跟踪名称,无论如何) ).

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).

要简单地将它们吹走,请手动将其删除或使用git checkout -f(强制)使git起作用.

To simply blow them away, remove them manually or use git checkout -f (force) to make git do it.

由于您现在不在分支上(处于分离的HEAD"模式),因此,如果要将它们永久提交到存储库中,可以使用类似CommuSoft在我撰写本文时添加的方法. (您可以在执行"git commit"之前或之后的任何时间创建新分支.)

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".)

您也可以使用git stash. Git的stash是看似复杂的小脚本:它所做的提交根本不在任何分支上,以后可以移植到分支中.使用它非常简单,容易:您只需运行git stash save并保存并清理所有未决的跟踪更改,或者运行git stash save -u并保存并清理所有待处理的跟踪更改 .现在,即使不是分支提交,他们也都可以安全地被提交到存储库中.

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 and untracked 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.

这里没有唯一的正确答案.

There is no single right answer for what to do here.

1 显然不同,因为如果您已经在提交中,则要求git将移至,则该文件将位于提交中并因此被跟踪,否则将不会提交,因此您不会要求git破坏它.

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.

2 这有点奇怪.如果我创建一个将被我的git checkout破坏的目录,并且将其作为一个 empty 目录,则git会继续对其进行破坏.在master^master之间的区别在于,从master^移到master会创建文件mxgroup.py(因此后退将其删除):

2This is a little odd. If I make a directory that will be clobbered by my git checkout, and I make it as an empty directory, 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):

$ 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

但是这是git版本2.0.2;也许旧的git并不那么聪明.

But this is with git version 2.0.2; perhaps older gits are not as clever.

这篇关于HEAD脱离原点/原版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆