Git 恢复上次分离的 HEAD

我在我的项目中遇到了一个大问题: 这就是场景。 我在 Git 下面有一个 xcode 项目。今天我意识到上一个提交破坏了一些测试,所以我检查了上一个提交。 我使用了 SourceTree,这是一个警告

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

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

我知道分离我的头是错误的,源树警告我... 但是有一种方法可以恢复我的工作?

34991 次浏览

If you type git reflog, it will show you the history of what revisions HEAD pointed to. Your detached head should be in there. Once you find it, do git checkout -b my-new-branch abc123 or git branch my-new-branch abc123 (where abc123 is 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.

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 -b or git branch to create a branch from that commit.

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

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?

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

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

In Sourcetree, you can do this using the 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.

Take that Commit ID to next step.

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!

enter image description here

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

git checkout -

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

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

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

 git cherry-pick 0b823d42..3cce27fc

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

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

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:

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.

  1. First, run git reflog to 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 e35d2b3 here e35dd23 is the hash value of your commit.
  4. That's it. Now just do git add . etc....

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