You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by switching back to a branch.
Oct 1, 2020. A detached HEAD occurs when you check out a commit that is not a branch. The term detached HEAD tells you that you are not viewing the HEAD of any repository. The HEAD is the most recent version of a branch. This is sometimes called the “tip of a branch”.
The "Detached Head" serves as a warning that you may also want to create or point to a branch if you intend to do any work from that point. But If you simply wish to view that tag or commit, there is nothing wrong with being in a detached head state.
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.
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 create new commits to your current branch, one commit per detached-HEAD-commit hash that you mention in the command. It also takes over the original commit messages.
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
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'
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With