在 git 中检索丢失的 HEAD 分支

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

Retrieve lost HEAD branch in git

gitversion-controlmerge

提问by sanjaypoyzer

I have a really simple Git project, with just a Master branch and no origins.

我有一个非常简单的 Git 项目,只有一个 Master 分支,没有起源。

Somewhere along the line I accidentally created a detached HEAD in my project, and then after making a bunch of commits on that detached HEAD, tried to merge it back into my Masterbranch. I was doing this in SourceTree, and at first the merge seemed to work (the graph showed master going into head, and there were merge conflicts which I fixed), but then I double clicked on Masterto switch to that branch and then suddenly my HEADdisappeared. I can't find it anywhere in SourceTree under All Branches, and git branchand git logon the command line only show my Master branch too.

沿着这条线的某个地方,我不小心在我的项目中创建了一个分离的 HEAD,然后在对那个分离的 进行了一堆提交之后HEAD,试图将它合并回我的Master分支。我在 SourceTree 中这样做,起初合并似乎工作(图表显示主进入头部,并且存在我修复的合并冲突),但随后我双击Master切换到该分支,然后突然我的HEAD消失了. 我在所有分支下的 SourceTree 中的任何地方都找不到它,git branch并且git log在命令行上也只显示我的主分支。

Is there anyway to get back my commits from my lost detached HEAD?

有没有办法从我丢失的 detached 中取回我的提交HEAD

回答by us_david

Here is what worked for me:

这是对我有用的:

  1. Use git reflogcommand to find out the head version that disappeared.
  2. Use git checkout @{-1}to move HEAD back to the one disappeared. The number -Xis how many commits the disappeared from current head. -1means the disappeared version is 1commit away from current HEAD.
  3. Create a branch to save the disappeared version
  1. 使用git reflog命令找出消失的头部版本。
  2. 使用git checkout @{-1}动脑子回一个消失了。数字-X是从当前头部消失的提交数量。-1意味着消失的版本是1从当前的 HEAD 提交的。
  3. 创建一个分支来保存消失的版本

回答by VonC

I can't find it anywhere in SourceTree

我在 SourceTree 的任何地方都找不到它

Go back to the command-line interface and do a git reflog.
You should be able to find back the SHA1 of your HEAD in that log.

返回命令行界面并执行git reflog
您应该能够在该日志中找到 HEAD 的 SHA1。

Note that you wouldn't have needed to "merge" your detached HEAD branch back to master.

请注意,您不需要将分离的 HEAD 分支“合并”回 master。

You simply could have reset master to your current detached HEAD commit.
See "How to fix a Git detached head?"

您只需将 master 重置为当前分离的 HEAD 提交。
请参阅“如何修复 Git 分离头?

git checkout master

In pure SourceTree fashion (as in this thread)

以纯 SourceTree 方式(如在此线程中

http://edwon.tv/wordpress/wp-content/uploads/2012/09/Screen-Shot-2012-10-15-at-4.15.22-PM.png

http://edwon.tv/wordpress/wp-content/uploads/2012/09/Screen-Shot-2012-10-15-at-4.15.22-PM.png

You could also have checked out an existing branch, then used right-click on the latest commit and selected Reset <branch>to this commit to move the branch pointer to where you want it to be.

When HEADis displayed, it means that you're on a detached HEAD, which means you have no branch checked out and any commits won't move any branches when they're made. If you checkout a detached HEAD in SourceTree, it will warn you fairly obviously, so maybe this was done on the command line beforehand and you saw the after effects in SourceTree.

您也可以签出现有分支,然后右键单击最新提交并选择Reset <branch>此提交以将分支指针移动到您想要的位置。

HEAD显示时,这意味着您在一个分离的 HEAD 上,这意味着您没有检出任何分支,并且任何提交在进行时都不会移动任何分支。如果你在 SourceTree 中检出一个分离的 HEAD,它会很明显地警告你,所以也许这是事先在命令行上完成的,你在 SourceTree 中看到了后期效果。

Alternative:

选择:

create a branch from my "HEAD" branch (which isn't really a branch at all) using the new branch command. At that point head is gone.
Then I was able to merge that branch with master and delete the unwanted branch I had created from HEAD.

HEAD使用 new branch 命令从我的“ ”分支(它根本不是一个分支)创建一个分支。那个时候头没了。
然后我能够将该分支与 master 合并并删除我从HEAD.