GIT恢复上次分离的HEAD [英] GIT restore last detached HEAD
问题描述
请在我的项目中遇到一个大问题:这是场景。
我有一个GIT下的xcode项目。今天我意识到最后一次提交会破坏一些测试,所以我检查了之前的提交。
我使用SourceTree,这是warnig
这样做会使您的工作副本成为'分离的HEAD',这意味着您将不再在分支上。如果你想在这之后提交,你可能想要再次签出一个分支,或者创建一个新的分支。这是正常的吗?
我工作了一整天,最后我承诺一切。
所以我需要合并我在开发分支上的工作,所以我检查开发分支,并且...我的工作立即消失:(b / b)
我知道错了分离我的头和Sourcetree警告我...但有一种方法可以恢复我的作品?
非常感谢。
HEAD / code>指向。你的分离的头应该在那里。一旦你找到它,做 git checkout -b my-new-branch abc123
或 git branch my-new-branch abc123
(其中 abc123
是detached HEAD的SHA-1)创建一个新分支,指向你已分离的头部,现在你可以在闲暇时合并该分支。
一般来说,如果你在分离的头部上工作后检查一个分支,Git应该告诉你来自已经分离的头部的提交,所以如果需要的话可以恢复它。我从来没有使用过SourceTree,所以我不会知道它是否转发该消息。但是如果它显示了这条消息,那么你应该可以使用它来找到提交,并再次使用 git checkout -b
或 git分支
从该提交中创建一个分支。
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 breaks some tests, so i checkout the previous commit.
I use SourceTree and this is the warnig
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?
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 disappear :(
I know was wrong to detach my HEAD and Sourcetree warned me...but there is a way to restore my works?
thanks a lot.
解决方案 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.
这篇关于GIT恢复上次分离的HEAD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!