可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
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.
回答2:
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!
回答3:
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.
回答4:
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?
回答5:
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);