修复一个 Git 分离头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10228760/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:31:01  来源:igfitidea点击:

Fix a Git detached head?

git

提问by Daniel

I was doing some work in my repository and noticed a file had local changes. I didn't want them anymore so I deleted the file, thinking I can just checkout a fresh copy. I wanted to do the Git equivalent of

我在我的存储库中做了一些工作,注意到一个文件有本地更改。我不再需要它们,所以我删除了该文件,以为我可以签出一个新的副本。我想做相当于的 Git

svn up .

Using git pulldidn't seem to work. Some random searching led me to a site where someone recommended doing

使用git pull似乎不起作用。一些随机搜索将我带到了一个有人推荐做的网站

git checkout HEAD^ src/

(srcis the directory containing the deleted file).

src是包含已删除文件的目录)。

Now I find out I have a detached head. I have no idea what that is. How can I undo?

现在我发现我有一个分离的头。我不知道那是什么。我怎样才能撤消?

回答by ralphtheninja

Detached head means you are no longer on a branch, you have checked out a single commit in the history (in this case the commit previous to HEAD, i.e. HEAD^).

分离头意味着你不再在一个分支上,你已经检查了历史中的一个提交(在这种情况下是 HEAD 之前的提交,即 HEAD^)。

If you want to deleteyour changes associated with the detached HEAD

如果要删除与分离的 HEAD 关联的更改

You only need to checkout the branch you were on, e.g.

您只需要结帐您所在的分支,例如

git checkout master

Next time you have changed a file and want to restore it to the state it is in the index, don't delete the file first, just do

下次你改变了一个文件,想恢复到它在索引中的状态时,不要先删除文件,直接做

git checkout -- path/to/foo

This will restore the file foo to the state it is in the index.

这会将文件 foo 恢复到它在索引中的状态。

If you want to keepyour changes associated with the detached HEAD

如果您想保持与分离的 HEAD 相关联的更改

  1. Run git branch tmp- this will save your changes in a new branch called tmp.
  2. Run git checkout master
  3. If you would like to incorporate the changes you made into master, run git merge tmpfrom the masterbranch. You should be on the masterbranch after running git checkout master.
  1. 运行git branch tmp- 这会将您的更改保存在名为tmp.
  2. git checkout master
  3. 如果您想将所做的更改合并到 中master,请git merge tmpmaster分支运行。master运行后你应该在分支上git checkout master

回答by Toni Gamez

If you have changed files you don't want to lose, you can push them. I have committed them in the detached mode and after that you can move to a temporary branch to integrate later in master.

如果您更改了不想丢失的文件,则可以推送它们。我已经在分离模式下提交了它们,之后你可以移动到一个临时分支,以便稍后在 master 中集成。

git commit -m "....."
git branch my-temporary-work
git checkout master
git merge my-temporary-work

Extracted from:

摘自:

What to do with commit made in a detached head

如何处理在分离头中进行的提交

回答by tanius

A solution without creating a temporary branch.

不创建临时分支的解决方案。

How to exit (“fix”) detached HEAD state when you already changed something in this modeand, optionally, want to save your changes:

当您已经在此模式下更改某些内容并且(可选)想要保存更改时,如何退出(“修复”)分离的 HEAD 状态:

  1. Commit changes you want to keep.If you want to take over any of the changes you made in detached HEAD state, commit them. Like:

    git commit -a -m "your commit message"
    
  2. Discard changes you do not want to keep.The hard reset will discard any uncommitted changes that you made in detached HEAD state:

    git reset --hard
    

    (Without this, step 3 would fail, complaining about modified uncommitted files in the detached HEAD.)

  3. Check out your branch.Exit detached HEAD state by checking out the branch you worked on before, for example:

    git checkout master
    
  4. Take over your commits.You can now take over the commits you made in detached HEAD state by cherry-picking, as shown in my answer to another question.

    git reflog
    git cherry-pick <hash1> <hash2> <hash3> …
    
  1. 提交您想要保留的更改。如果您想接管在分离 HEAD 状态下所做的任何更改,请提交它们。喜欢:

    git commit -a -m "your commit message"
    
  2. 丢弃您不想保留的更改。硬重置将丢弃您在分离的 HEAD 状态下所做的任何未提交的更改:

    git reset --hard
    

    (没有这个,第 3 步就会失败,抱怨分离的 HEAD 中修改了未提交的文件。)

  3. 检查你的分支。通过检查您之前处理过的分支来退出分离的 HEAD 状态,例如:

    git checkout master
    
  4. 接管你的承诺。您现在可以通过挑选来接管您在分离 HEAD 状态下所做的提交,如我对另一个问题的回答所示。

    git reflog
    git cherry-pick <hash1> <hash2> <hash3> …
    

回答by Razan Paul

Detached head means:

分离头是指:

  1. You are no longer on a branch,
  2. You have checked out a single commit in the history
  1. 你不再在树枝上,
  2. 您已检出历史记录中的单个提交

If you have no changes:you can switch to master by applying the following command

如果您没有任何更改:您可以通过应用以下命令切换到 master

  git checkout master

If you have changes that you want to keep:

如果您有要保留的更改:

In case of a detached HEAD, commits work like normal, except no named branch gets updated.?To get master branch updated with your committed changes, make a temporary branch where you are (this way the temporary branch will have all the committed changes you have made in the detached HEAD), then switch to the master branch and merge the temporary branch with the master.

在分离的 HEAD 的情况下,提交工作正常,除了没有命名分支被更新。?为了让主分支更新你提交的更改,在你所在的地方创建一个临时分支(这样临时分支将拥有你提交的所有更改已在分离的 HEAD 中创建),然后切换到 master 分支并将临时分支与 master 合并。

git branch  temp
git checkout master
git merge temp

回答by Philippe Gerber

Here's what I just did after I realized I was on a detached head and had already made some changes.

这是我在意识到自己处于一个超脱的头脑并且已经做了一些改变之后刚刚做的事情。

I committed the changes.

我提交了更改。

$ git commit -m "..."
[detached HEAD 1fe56ad] ...

I remembered the hash (1fe56ad) of the commit. Then I checked out the branch I should have been on.

我记得提交的哈希值 (1fe56ad)。然后我检查了我应该在的分支。

$ git checkout master
Switched to branch 'master'

Finally I applied the changes of the commit to the branch.

最后我将提交的更改应用到分支。

$ git cherry-pick 1fe56ad
[master 0b05f1e] ...

I think this is a bit easier than creating a temporary branch.

我认为这比创建一个临时分支要容易一些。

回答by mojuba

If you made some changes and then realized that you are on a detached head, there is a simple solution for that: stash -> checkout master -> stash pop:

如果您进行了一些更改,然后意识到您处于分离状态,则有一个简单的解决方案:stash -> checkout master -> stash pop:

git stash
git checkout master   # Fix the detached head state
git stash pop         # Or for extra safety use 'stash apply' then later 
                      #   after fixing everything do 'stash drop'

You will have your uncommited changes and normal "attached" HEAD, like nothing happened.

您将拥有未提交的更改和正常的“附加” HEAD,就像什么也没发生一样。

回答by larsks

When you check out a specific commit in git, you end up in a detached headstate...that is, your working copy no longer reflects the state of a named reference (like "master"). This is useful for examining the past state of the repository, but not what you want if you're actually trying to revert changes.

当您检出 中的特定提交时git,您最终处于分离的头部状态……也就是说,您的工作副本不再反映命名引用的状态(如“master”)。这对于检查存储库的过去状态很有用,但如果您实际上尝试还原更改,则不是您想要的状态。

If you have made changes to a particular file and you simply want to discard them, you can use the checkoutcommand like this:

如果您对特定文件进行了更改并且只想放弃它们,则可以使用如下checkout命令:

git checkout myfile

This will discard any uncommitted changes and revert the file to whatever state it has in the head of your current branch. If you want to discard changes that you have already committed, you may want to use the resetcommand. For example, this will reset the repository to the state of the previous commit, discarding any subsequent changes:

这将丢弃任何未提交的更改并将文件恢复到它在当前分支头部的任何状态。如果您想放弃已经提交的更改,您可能需要使用该reset命令。例如,这会将存储库重置为上一次提交的状态,丢弃任何后续更改:

git reset --hard HEAD^

However, if you are sharing the repository with other people, a git resetcan be disruptive (because it erases a portion of the repository history). If you have already shared changes with other people, you generally want to look at git revertinstead, which generates an "anticommit" -- that is, it creates a new commit that "undoes" the changes in question.

但是,如果您与其他人共享存储库,则git reset可能会造成破坏(因为它会删除部分存储库历史记录)。如果您已经与其他人共享了更改,您通常希望查看git revert它,它会生成一个“反提交”——也就是说,它会创建一个新的提交来“撤消”有问题的更改。

The Git Bookhas more details.

Git Book有更多细节。

回答by MarianD

HEAD is in a pointer, and as a result it points — directly or indirectly— to a particular commit:

HEAD 在一个指针中,因此它直接或间接地指向一个特定的提交:

Attached? HEAD means that it is attached to some branch(i.e it pointsto a branch).
DetachedHEAD means that it is notattached to any branch, i.e. it points directlyto some commit.

附上?HEAD 意味着它附加到某个分支(即它指向一个分支)。
分离的HEAD 意味着它附加到任何分支,即它直接指向某个提交。

enter image description here

在此处输入图片说明

In other words:

换句话说:

  • If it points to a commit directly, the HEAD is detached.
  • If it points to a commit indirectly, (i.e. it points to a branch, which in turn points to a commit), the HEAD is attached.
  • 如果它直接指向一个提交,则 HEAD 是分离的
  • 如果它间接指向一个提交(即它指向一个分支,它又指向一个提交),则附加HEAD 。


To better understand situations with attached / detached HEAD, let's show the steps leading to the quadruplet of pictures above.

为了更好地理解附加/分离 HEAD 的情况,让我们展示导致上述四组图片的步骤。

We begin with the same state of the repository (pictures in all quadrants are the same):

我们从存储库的相同状态开始(所有象限中的图片都相同):

enter image description here

在此处输入图片说明



Now we want to perform git checkout— with different targets in the individual pictures (commands on top of them are dimmed to emphasize that we are only going toapply those commands):

现在,我们要执行git checkout-与各个画面不同的目标(在它们上面的命令将变灰强调的是,我们只应用这些命令):

enter image description here

在此处输入图片说明



This is the situation afterperforming those commands:

这是执行这些命令的情况:

enter image description here

在此处输入图片说明

As you can see, the HEAD points to the targetof the git checkoutcommand — to a branch(first 3 images of the quadruplet), or (directly) to a commit(the last image of the quadruplet).

正如可以看到,头部指向目标的的git checkout命令-的分支(四联的第一3个图像),或者(直接地)给一个提交(四联的最后一个图像)。

The content of the working directory is changed, too, to be in accordance with the appropriate commit (snapshot), i.e. with the commit pointed (directly or indirectly) by the HEAD.

工作目录的内容也被更改,以符合适当的提交(快照),即由 HEAD(直接或间接)指向的提交。



So we are now in the same situation as in the start of this answer:

因此,我们现在处于与此答案开头相同的情况:

enter image description here

在此处输入图片说明

回答by Mike

Since "detached head state" has you on a temp branch, just use git checkout -which puts you on the last branch you were on.

由于“分离的头部状态”让您处于临时分支,因此只需使用git checkout -它即可将您置于您所在的最后一个分支。

回答by Timo

To further clarify @Philippe Gerber's answer, here it is:

为了进一步澄清@Philippe Gerber 的回答,这里是:

git cherry-pick

git 樱桃挑选

Before cherry-pick, a git checkout masteris necessary in this case. Furthermore, it is only needed with a commitin detached head.

在 之前cherry-pickgit checkout master在这种情况下a是必需的。此外,它只需要一个commitin detached head