I see everyone almost everyone has suggested a solution where a temporary branch is being created. Now, one needs to admit that whenever this "committed in a detached state" issue arises, it's generally detected after one commit. And creating one whole branch for that one puny commit - Sounds too much, right? Especially in projects where you are already jumping around among too many branches.
What's the easy way then? Use the commit hash!
How do I get that?
- Do a
git log
. You would see something like this:
commit 10bf8fe4d17bb7de59586a7abb6db321f0786bb3 (HEAD)
Author: Someone <someone@something.com>
Date: So/me/day SO:ME:TI:ME
A commit message that doesn't mean much
commit a3cd1cedf1962916cdc2945f2bd2b271ec8b919d (origin/master, master)
Author: Someone <someone@something.com>
Date: Some/other/day SOME:OTHER:TIME
Another commit message that doesn't mean much
commit 1bfabbe09c70419070fe29ff1ed276c0207bbe10
Author: Someone <someone@something.com>
Date: Thu Jul 8 08:38:12 2021 +0530
Enough reading the example, focus on the answer!!
Now, although it looks like a normal case, but when you do a git push
it would say "Everything up-to-date".
A careful person would see that it's not "up-to-date". HEAD
is somewhere other than the master.
- So, what next? Just copy the initial chars of the hash
10bf8fe4d1
. And first, do a git checkout master
. This would move you to master
branch. And now since you have already copied the hash. You can do a git merge <hash>
. Do a git log
now
And VOILA:
commit 10bf8fe4d17bb7de59586a7abb6db321f0786bb3 (HEAD -> master)
Author: Someone <someone@something.com>
Date: S/om/eday SO:ME:TI:ME
A commit message that doesn't mean much
commit a3cd1cedf1962916cdc2945f2bd2b271ec8b919d (origin/master)
Author: Someone <someone@something.com>
Date: Some/other/day SOME:OTHER:TIME
Another commit message that doesn't mean much
commit 1bfabbe09c70419070fe29ff1ed276c0207bbe10
Author: Someone <someone@something.com>
Date: Thu Jul 8 08:38:12 2021 +0530
Enough reading the example, focus on the answer!!
Now, the HEAD
seems to be in a proper place.
Someone might ask, "What if I don't have the hash? I didn't know anything about the dangling commit and just did a git checkout master
." Don't worry, I've got you covered. You can find the commit hash in two places:
- When you did the
git checkout master
, git
had warned you like this
Warning: you are leaving 1 commit behind, not connected to
any of your branches:
10bf8fe A commit message that doesn't mean much
If you want to keep it by creating a new branch, this may be a good time
to do so with:
git branch <new-branch-name> 10bf8fe
Switched to branch 'master'
You can see your treasure (hash
), right?
- Don't tell me you can't find it. It's right there. But if you really can't, then you can do a
git reflog
. It will show you something like this:
a3cd1ce (HEAD -> master, origin/master) HEAD@{0}: checkout: moving from 10bf8fe4d17bb7de59586a7abb6db321f0786bb3 to master
10bf8fe HEAD@{1}: commit: A commit message that doesn't mean much
You see that there's the treasure you were looking for... The hash.
I guess this covers all possible scenarios that could happen in a detached state with a dangling commit. Beware next time!!