What happens to git commits created in a detached HEAD state?

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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:

  1. Look up the SHA-1 hashes of the commits you made in detached HEAD state with

     git reflog
    
  2. 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.

Solution 3 - Git

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

Solution 4 - Git

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'

Solution 5 - Git

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.

Solution 6 - Git

Follow these steps to link your detached head back to git repo

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

  1. git reflog get the commit SHA's listed from your commit list of detached branch.

  2. git cherry-pick "commit hash1" "commit hash2" "commit hash3"

  3. git push

ALL SET!!

Solution 7 - Git

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!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMausimoView Question on Stackoverflow
Solution 1 - GitDietrich EppView Answer on Stackoverflow
Solution 2 - GittaniusView Answer on Stackoverflow
Solution 3 - GitsergtkView Answer on Stackoverflow
Solution 4 - GitGreg BaconView Answer on Stackoverflow
Solution 5 - GitGreg HewgillView Answer on Stackoverflow
Solution 6 - Gituser1520615View Answer on Stackoverflow
Solution 7 - GitblalondView Answer on Stackoverflow