Have you ever panicked while using Git, thinking, "Huh? The changes I committed are gone...?" I just made this mistake recently and had a bit of a panic myself 😳
What is "detached HEAD"? My blunder experience
One day, I wanted to test an old commit, so I used git checkout to go back to an older state. Then I ended up in this "detached HEAD" state, and it seemed like I wasn't on my usual branch anymore 💭
Without realizing it, I made some changes and even committed them, but when I returned to my original branch...
"Wait, where did my recent commit go?" 😱
Turns out, commits made in a detached HEAD state aren't attached to any regular branch, so they disappear from view the moment you switch back 💦
But don't worry! Do you know about git reflog?
Enter the savior ✨
The git reflog command might seem super low-key, but it records all your Git operations 🔥
Even "lost" commits—reflog tracks their movements, so there's a good chance you can recover them!
How do you recover them?
- Run
git reflogin your terminal, and you'll see a list of recent HEAD movements 👀 - Look for the hash (ID) of the lost commit
- Use
git checkout [hash]to return to that state - If you want to bring it back to a branch, create a new branch with
git branch [branch_name]and you're good
This way, you can rescue "vanished" commits ✨
Summary
Git is super useful, but it can be a bit intimidating when you're still learning 💭
But knowing about these "hidden features" that can cover your slip-ups makes things way less stressful 👍
If you ever lose a commit, remember to try git reflog first 🌸
💡 For detailed recovery steps, check out this link too 👇
detached stateとreflogでの復旧方法
Comments
クリス
`git log --graph --oneline --reflog` is much easier to understand than `git reflog`.
ハンナ
This person just keeps promoting their Medium blog on /r/git.
エイダン
https://ohshitgit.com/ (as is)
リリー
reflog has saved me many times. Just be careful with regular git gc, it removes commits that are detached from the tree.
ハンナ
This feels like an ad for sorcery. Git's problem is overemphasizing branches. Commits are the actual work units, but talking about 'temporary commits' is weird.
ノーラン
For when the first whipping doesn't work.
ワット
Yeah, but that's only until garbage collection comes around.








