Reputation: 8168
This is what happened:
I have a branch A. On branch A I committed a bunch of changes. I was not happy with the code, so I checked out the previous commit in branch A. I then made a bunch more changes and committed them on branch A. Now I can not find this commit anywhere. Did I lose this code?
Upvotes: 201
Views: 73089
Reputation: 16829
Your commits are still available in the reflog, as pointed out already. In addition to the other answers, here is a way to take over the detached HEAD commits into your current branch directly, without creating and merging a new branch:
Look up the SHA-1 hashes of the commits you made in detached HEAD state with
git reflog
Then execute, with all the commit hashes ordered from oldest to most recent:
git cherry-pick <hash1> <hash2> <hash3> ...
For example if I had only one, given in the "first 7 characters" short hash format:
git cherry-pick a21d053
This will add new commits to your current branch, one commit per detached-HEAD commit that you mention in the command. It also takes over the original commit messages.
Upvotes: 107
Reputation: 111
Follow these steps to link your detached head back to git repo
git checkout "your branch with path but without remote name"
e.g. if remote name is origin and branch name is bugfix/somebranch
then use git checkout bugfix/somebranch
git reflog
get the commit SHA's listed from your commit list of detached branch.
git cherry-pick "commit hash1" "commit hash2" "commit hash3"
git push
ALL SET!!
Upvotes: 11
Reputation: 139631
Git parlance for the state of your working directory is a “detached HEAD.” Here is another place that git reflog
makes the save.
$ git reflog
0b40dd6 HEAD@{0}: commit: my commit on detached HEAD
...
If I try to checkout a different branch, git-1.7.5.1 gives a helpful suggestion.
$ git checkout master Warning: you are leaving 1 commit behind, not connected to any of your branches: 0b40dd6 my commit on detached HEAD 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 0b40dd65c06bb215327863c2ca10fdb4f904215b Switched to branch 'master'
Upvotes: 11
Reputation: 10974
You may find lost (dangling) commits with the following command:
git fsck --lost-found
Note, if your current head is dangling commit, it is not listed as lost.
You may find more info at git-fsck(1) Manual Page
Then you may create branch on that lost commit:
git branch new-branch ba5a739
Upvotes: 17
Reputation: 885
In Sourcetree, I found that git reflog didn't work, so I figured out how to do this using the GUI.
First, try to find the "lost" commit by looking for a message in the Command History (view:Show Command Output). It'll hopefully be in the command "Switching Branch" after the commit that you lost and 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!
This brought back some lost work for me!
Upvotes: 3
Reputation: 213578
The old commit is still in the reflog.
git reflog
This will show a list of commits, and the "lost" commit should be in there. You can make it into a new branch. For example, if the SHA-1 is ba5a739, then you can make a new branch named "new-branch" at the old commit with:
git branch new-branch ba5a739
Note that "lost" commits will get deleted when the database is pruned.
Upvotes: 285
Reputation: 994051
You did not lose it, Git still keeps a copy (but it is currently unreachable by any branch head). You can find your missing commit using the git reflog
command. The reflog keeps track of the historical positions of a branch head, and you can use it to find things that the branch head was pointing at previously.
Upvotes: 9