简体   繁体   English

在git中检索丢失的HEAD分支

[英]Retrieve lost HEAD branch in git

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 Master branch. 沿着这条线的某个地方,我不小心在项目中创建了一个分离的HEAD,然后在对该分离的HEAD进行一堆提交之后,尝试将其合并回我的Master分支。 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 Master to switch to that branch and then suddenly my HEAD disappeared. 我是在SourceTree中进行此操作的,起初合并似乎可以正常工作(该图显示master进入了头部,并且我已解决了合并冲突),但是随后我双击Master切换到该分支,然后突然HEAD消失了。 I can't find it anywhere in SourceTree under All Branches, and git branch and git log on the command line only show my Master branch too. 我在SourceTree的All Branches下的任何地方都找不到它,并且命令行上的git branchgit log只显示我的Master分支。

Is there anyway to get back my commits from my lost detached HEAD ? 反正从我失去了分离找回我提交HEAD

Here is what worked for me: 这对我有用:

  1. Use git reflog command to find out the head version that disappeared. 使用git reflog命令找出消失的头部版本。
  2. Use git checkout @{-1} to move HEAD back to the one disappeared. 使用git checkout @{-1}将HEAD移回消失的那个。 The number -X is how many commits the disappeared from current head. -X是从当前头部消失的提交次数。 -1 means the disappeared version is 1 commit away from current HEAD. -1表示消失的版本与当前HEAD相距1提交。
  3. Create a branch to save the disappeared version 创建一个分支来保存消失的版本

I can't find it anywhere in SourceTree 我在SourceTree的任何地方都找不到

Go back to the command-line interface and do a git reflog . 返回命令行界面并执行git reflog
You should be able to find back the SHA1 of your HEAD in that log. 您应该能够在该日志中找到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. 您可以简单地将master重置为当前的分离HEAD提交。
See " How to fix a Git detached 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

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. 您也可能已经签出了一个现有分支,然后右键单击最新的提交,然后选择Reset <branch>到该提交,以将分支指针移动到您想要的位置。

When HEAD is 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. HEAD显示,这意味着你在一个分离的头,这意味着你没有分支签出,它们是由时任提交不会动任何分支机构。 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. 如果您在SourceTree中签出分离的HEAD,它将明显警告您,因此也许这是事先在命令行上完成的,并且您在SourceTree中看到了After效果。

Alternative: 选择:

create a branch from my " HEAD " branch (which isn't really a branch at all) using the new branch command. 使用新的branch命令从我的“ HEAD ”分支(根本不是分支)创建一个分支。 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 . 然后,我能够将该分支与master合并,并删除从HEAD创建的不需要的分支。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM