拡張機能研究所

Introducing recommended browser extensions in manga format!

2025/11/05 16:00

Rescuing Lost Commits in Git!? The Hidden Secrets of 'git reflog'

When you touch an old commit and it disappears... Here's a quick introduction to using 'git reflog' and how to revive lost commits 💡

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?

  1. Run git reflog in your terminal, and you'll see a list of recent HEAD movements 👀
  2. Look for the hash (ID) of the lost commit
  3. Use git checkout [hash] to return to that state
  4. 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での復旧方法

Show animated messageON
Phew~ Glad you're safe 💗

Comments

Ataror of Christian

クリス

`git log --graph --oneline --reflog` is much easier to understand than `git reflog`.

Ataror of Brooklynn

ハンナ

This person just keeps promoting their Medium blog on /r/git.

Ataror of Aidan

エイダン

https://ohshitgit.com/ (as is)

Ataror of Luis

リリー

reflog has saved me many times. Just be careful with regular git gc, it removes commits that are detached from the tree.

Ataror of Brooklynn

ハンナ

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.

Ataror of Nolan

ノーラン

For when the first whipping doesn't work.

Ataror of Wyatt

ワット

Yeah, but that's only until garbage collection comes around.


PICKUP
Related Articles