Free — no signup required

The Safety Net: Git Reflog

2 min read

"Help! I Lost My Commits!"

Commands like git rebase, git reset --hard, and git commit --amend are "destructive" in the sense that they move branch pointers away from commits. If you make a mistake, it looks like your work is gone — the branch no longer points to those commits, so they don't appear in git log.

The reflog (reference log) is Git's secret journal. Every time HEAD moves — checkout, commit, rebase, reset, merge — Git appends an entry to .git/logs/HEAD recording where HEAD was and where it moved to, building a chronological list of every position HEAD has ever occupied, each labeled with what kind of operation caused the move.

Recovering from a Disaster

Suppose a hard reset was run by mistake and five commits appear to be lost. The recovery is to walk the reflog back to the entry just before the mistake, then reset back to that point — since the commits were never actually deleted from the object database, they just had no pointer referencing them anymore. Once the branch pointer moves back to that entry, the "lost" work is exactly where it was.

Important Limitations

The reflog is local to your machine. It is not pushed to remote servers. If you clone a repository or your colleague's machine crashes, the reflog is not available. This is why the reflog saves you from your own mistakes but cannot recover work that was never committed or pushed.

Reflog entries also expire. By default, entries older than 90 days (or 30 days for unreachable commits) are pruned during git gc. For most recovery scenarios this is more than sufficient, but it means the reflog is not an indefinite backup.

Interview Tip

Interviewers testing Git depth often ask: "You ran git reset --hard and lost commits. How do you recover?" Many candidates say "you can't." The correct answer is git reflog — find the commit hash from before the reset and git reset --hard <hash> back to it. Follow up: "What if the reflog is also gone?" Then git fsck --lost-found can find dangling objects in the object database. This two-layer answer demonstrates genuine depth.

Key Point: The reflog records every HEAD movement locally, making it the ultimate undo button for destructive local operations. It is not replicated to remotes and has a default expiry of 90 days, so it protects against mistakes but is not a backup strategy.

This is one of 18 chapters

Every chapter is free — Kubernetes, Terraform, SRE, distributed systems, and more — with fast daily review built in.

Sign up free