GIT 恢复上次分离的 HEAD

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

GIT restore last detached HEAD

git

提问by IgnazioC

Please, I have a big problem in my project: this is the scenario. I have an xcode project under GIT. Today I realized that the last commit broke some tests, so i checked out the previous commit. I used SourceTree and this is the warning

拜托,我的项目有一个大问题:这就是场景。我在 GIT 下有一个 xcode 项目。今天我意识到最后一次提交破坏了一些测试,所以我检查了以前的提交。我使用了 SourceTree,这是警告

Doing so will make your working copy a 'detached HEAD', which means you won't be on a branch anymore. If you want to commit after this you'll probably want to either checkout a branch again, or create a new branch. Is this ok?

这样做会使您的工作副本成为“分离的 HEAD”,这意味着您将不再位于分支上。如果您想在此之后提交,您可能需要再次签出一个分支,或者创建一个新分支。这个可以吗?

I worked for an entire day and at the end I committed everything. So I needed to merge my work on develop branch so I checkout the develop branch and...my work instantly disappeared :(

我工作了一整天,最后我承诺了一切。所以我需要合并我在开发分支上的工作,所以我检查了开发分支......我的工作立即消失了:(

I know it was wrong to detach my HEAD and Sourcetree warned me...but there is a way to restore my work?

我知道分离我的 HEAD 是错误的,Sourcetree 警告我......但是有办法恢复我的工作吗?

回答by Brian Campbell

If you type git reflog, it will show you the history of what revisions HEADpointed to. Your detached head should be in there. Once you find it, do git checkout -b my-new-branch abc123or git branch my-new-branch abc123(where abc123is the SHA-1 of the detached HEAD) to create a new branch that points to your detached head. Now you can merge that branch at your leisure.

如果您键入git reflog,它将显示修订所HEAD指向的历史记录。你分离的头应该在那里。找到它后,执行git checkout -b my-new-branch abc123git branch my-new-branch abc123abc123分离的 HEAD 的 SHA-1在哪里)创建一个指向分离的头部的新分支。现在您可以在闲暇时合并该分支。

Generally, if you check out a branch after working on a detached head, Git should tell you the commit from the detached head you had been on, so you can recover it if you need. I've never used SourceTree, so I don't know if it relays that message. But if it did display that message, then you should be able to use that to find the commit, and again use git checkout -bor git branchto create a branch from that commit.

通常,如果您在处理分离头后检出分支,Git 应该告诉您来自分离头的提交,您可以在需要时恢复它。我从未使用过 SourceTree,所以我不知道它是否会转发该消息。但是如果它确实显示了该消息,那么您应该能够使用它来查找提交,并再次使用git checkout -bgit branch从该提交创建一个分支。

回答by blalond

In Sourcetree, you can do this using the GUI.

在 Sourcetree 中,您可以使用 GUI 执行此操作。

First find the "lost" commit by looking for a message in the Command History (view:Show Command Output). It will probably be in the command "Switching Branch" after the commit that you lost. In that message, hopefully you'll see the commit comment with a 1234567 commit ID.

首先通过在命令历史记录中查找消息来找到“丢失”的提交(查看:显示命令输出)。在您丢失的提交之后,它可能会在命令“切换分支”中。在该消息中,希望您会看到带有 1234567 提交 ID 的提交注释。

Take that Commit ID to next step.

将该提交 ID 带到下一步。

Hit the "Branch" button in the top toolbar and you should get a dialog "New Branch" where you can specify a certain commit. Put that Commit ID in there, specify a new branch name, hit Create Branch and you should get a new branch with your lost commit!

点击顶部工具栏中的“分支”按钮,您应该会看到一个对话框“新分支”,您可以在其中指定某个提交。把那个提交 ID 放在那里,指定一个新的分支名称,点击创建分支,你应该得到一个丢失提交的新分支!

enter image description here

在此处输入图片说明

回答by Desert Rose

If you dont want to keep changes of detached HEAD and want to go to latest branch commit use below command directly.

如果您不想保留分离的 HEAD 的更改并想直接转到最新的分支提交,请直接使用下面的命令。

git checkout - 

Note:I will delete all your changes in the detached HEAD.

注意:我将删除您在分离的 HEAD 中的所有更改。

回答by manuelvigarcia

A colleague of mine just had this situation. In his case, there were commits in detached head --they work in R-Studio-- and the tool did warn them that they could create the branch with this and that SHA reference... but since the only option was "Close" --duh!! it was a info box-- they closed the dialogue and lost the info for ever...

我的一个同事刚好遇到这种情况。在他的情况下,在分离头中有提交——他们在 R-Studio 中工作——并且该工具确实警告他们他们可以使用这个和那个 SHA 引用创建分支......但因为唯一的选择是“关闭” ——呸!!这是一个信息框——他们关闭了对话并永远丢失了信息......

Thanks to the reflogcommand we could see that the changes were not lost. But in our case, the git branchdid not work as expected... or a incoming git pulldid mess it up somehow. We had to fish the changes from the reflog to the newly created branch:

由于该reflog命令,我们可以看到更改没有丢失。但在我们的例子中,git branch没有按预期工作......或者传入git pull确实以某种方式搞砸了。我们不得不从 reflog 到新创建的分支进行更改:

 git cherry-pick 0b823d42..3cce27fc

which placed all the commits we wanted in the branch. Then we could merge the branch into developwithout issues.

它将我们想要的所有提交放在分支中。然后我们可以develop毫无问题地合并分支。

Just in case this is informative for anyone, we did identify the commits on detached headin the reflogby looking at those in between the marked with "checkout" (which identify branch shifting):

万一这是信息的人,我们也确定在提交分离的头reflog由标有“结账”之间的看着那些(其识别分支转移):

e09f183b HEAD@{3}: pull: Fast-forward
b5bf3e1d HEAD@{4}: checkout: moving from lost_changes to develop
b5bf3e1d HEAD@{5}: checkout: moving from 3cce27fca50177a288df0252f02edd5da5ee64fd to lost_changes
3cce27fc HEAD@{6}: commit: add statistics
417a99a4 HEAD@{7}: commit: add test
0b823d42 HEAD@{8}: commit: new utility class
d9ea8a63 HEAD@{9}: checkout: moving from develop to d9ea8a635d4c2349fcb05b3339a6d7fad5ae2a09
b5bf3e1d HEAD@{10}: pull: Fast-forward

Those we wanted were HEAD@{8}to HEAD@{6}(both inclusive). So we got them by:

那些我们需要做的是HEAD@{8}HEAD@{6}(包括两端)。所以我们得到了它们:

git cherry-pick 0b823d42..3cce27fc

Then the usual merge solving and final commit left us with branch lost_changes hosting the detached-head work that we thought lost. Merging that into develop was fast-forward this time.

然后通常的合并解决和最终提交给我们留下了分支 lost_changes 托管我们认为丢失的分离头工作。这次将其合并到开发中是快进的。

回答by sohaib

  1. First, run git reflogto view history.
  2. The oldest revision will be the last one in the list.
  3. Switch to your desired commit using git checkout -b temp e35d2b3here e35dd23 is the hash value of your commit.
  4. That's it. Now just do git add . etc....
  1. 首先,运行git reflog查看历史记录。
  2. 最早的修订将是列表中的最后一个。
  3. 使用git checkout -b temp e35d2b3这里切换到您想要的提交e35dd23 是您提交的哈希值。
  4. 就是这样。现在只需执行 git add 。等等....

Accept it as an answer if it solves your issue. Otherwise please share your comment.

如果它解决了您的问题,请接受它作为答案。否则,请分享您的评论。

回答by xvorsx

I tried this scenario, and find that git tell me SHA-1 of last commit:

我尝试了这个场景,发现 git 告诉我上次提交的 SHA-1:

vors@localhost:~/git-test$ git checkout master 
Warning: you are leaving 1 commit behind, not connected to
any of your branches:

  ec600e6 333

If you want to keep them by creating a new branch, this may be a good time
to do so with:

 git branch new_branch_name ec600e6eb2473dd4f3732539c5c1fa5829f631b7

Switched to branch 'master'

Did you see this message?

你看到这条消息了吗?

回答by forvaidya

detached head is fine as long as you want to make No change.

只要你想不改变,分离头就可以了。

If you want revert a commit, you can use git revert on specific branch

如果要还原提交,可以在特定分支上使用 git revert

If you want to work off detached head and do commits; create a new branch (and later merge it);

如果你想脱离独立的头部并进行提交;创建一个新分支(然后合并它);