Coding/Learn/git/git reflog and disaster recovery
Git & GitHub Complete Course
01.Introduction, Architecture & Configuration
02.Staging Area, atomic Commits & git status
03.Branching, Fast-Forward & 3-Way Merging
04.Stash, Log Visualization & History Inspection
05.Workflows (Git Flow, Trunk-Based) & Semantic Tagging
06.SSH Keys, Remote Repositories & Origin Setup
07.Push, Fetch, Pull & Upstream Tracking Branches
08.Forking, Clones & Open-Source Pull Requests (PRs)
09.GitHub Flow, Branch Protection & GitHub Pages
10.Undoing Changes: git restore & Safe Public git revert
11.Reset: Deep Dive into --soft, --mixed & --hard
12.Commit Amending & Interactive Rebase (git rebase -i)
13.Reflog & Disaster Recovery (Reviving "Lost" Commits)
14.Merge Conflicts Resolution & git rerere
15..gitignore Rules & .gitattributes Cross-Platform Handling
16.Hooks & Automated Quality Assurance (Pre-Commit)
17.Submodules & Large File Storage (Git LFS)
18.GPG Cryptographic Signing & git cherry-pick
19.Advanced Remotes, Garbage Collection (git gc) & Maintenance
20.Capstone: Production Multi-Branch Enterprise Release Pipeline
Course Certificate0%

Complete all lessons to earn

LearnGit Reflog & Disaster Recovery (Reviving "Lost" Commits)
13 / 20 0%
Lesson 13 of 20
6 mins read

Git Reflog & Disaster Recovery (Reviving "Lost" Commits)

PreviousNext Lesson
In Plain English

In Git, almost nothing is truly lost. The Reference Log (git reflog) records every single movement of the HEAD pointer over the last 30 to 90 days. Even after an accidental git reset --hard or deleted branch, you can recover lost commits instantly using reflog pointers.

The Mental Model

"The reflog is an airplane's black box flight recorder: even if you accidentally crash your branch pointer, the black box recorded every single step and lets you rewind time to the exact second before the accident."

Deep Dive: How It Works

HEAD@{n} Notation: Represents the state of HEAD n pointer movements ago.

Dangling Commits: Commits detached from branch pointers remain safe in .git/objects until garbage collection.

Restoration via Branch: git branch recovered-branch HEAD@{2} resurrects a lost commit into a fresh branch.

Syntax Blueprint

git reflog
# Identify commit SHA before accidental reset
git reset --hard HEAD@{1}
# Or create recovery branch:
git branch rescue HEAD@{1}

Inspect reflog movement history and restore HEAD or create branch at target timestamp.

PointCore Rules to Remember

Point star
Local Safety Net: Reflog is strictly local to your machine and tracks all checkouts, commits, rebases, and resets.
Point star
Garbage Collection Buffer: Git retains unreachable commits for 30 days by default (gc.pruneExpire).
Point star
Detached HEAD Recovery: Easily turn experimental commits created in detached HEAD into a permanent branch.

Common Beginner Traps & How to Fix Them

Mistake:Panicking after an accidental git reset --hard and deleting the entire repository directory.

Why it happens: Believing data is permanently erased.

How to fix: Run git reflog immediately to identify the previous commit hash and restore it.

Pro Tip: You can inspect individual branch movement logs as well: git reflog show feature/auth.

Live Interactive Example

Hit Run Code to see it live
Disaster Recovery via Reflog
Git 2.45 / CLI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Output Console
Click "Run Code" to view the rendered output.
How it works: git reflog tracks the commit before reset, allowing instant 100% recovery with HEAD@{1}.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Recover Lost Commit with Reflog

  • PointRecover the lost commit by resetting to "HEAD@{1}" using "git reset --hard HEAD@{1}".
  • PointVerify content of data.txt with "cat data.txt".
1
2
3
4
5
6
7
8
9
10
11
12
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates