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
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.
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!
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.
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);
git reflog
to view history. git checkout -b temp e35d2b3
here e35dd23 is the hash value of your commit.Accept it as an answer if it solves your issue. Otherwise please share your comment.