git reflog — The Developer Safety Net
How to rescue lost commits and branches with git reflog: reference logs, recovering from accidental git reset --hard, and recovering deleted branches.
git reflog — The Developer Safety Net
git reflog (Reference Logs) is Git's ultimate safety net. It records every single update to the HEAD pointer on your local machine—including commits, branch switches, resets, and rebases.
Even if you run git reset --hard and think you destroyed your commits, the commits still exist in the Git object store for at least 30 to 90 days.
1. Viewing the Reference Log
git reflogOutput:
39efcea (HEAD -> main) HEAD@{0}: reset: moving to 39efcea
8a1b2c3 HEAD@{1}: commit: feat: awesome feature that got deleted
7b4c901 HEAD@{2}: checkout: moving from feat/auth to mainEvery entry shows:
- Commit SHA hash (
8a1b2c3) HEAD@{n}index (how many moves ago it occurred)- Action description
2. Rescuing Lost Commits
If you accidentally wiped feat: awesome feature with a hard reset:
- Run
git reflogto locate the lost commit hash (8a1b2c3). - Point your current branch back to that commit, or create a new branch from it:
# Option A (Recommended): Create a new rescue branch at that commit git branch rescued-work 8a1b2c3 # Option B: Reset your current branch directly back to that state git reset --hard 8a1b2c3
Related Topics
git push — Uploading Local Commits to Remotes
Uploading commits with git push: upstream tracking (-u), safe force pushes (--force-with-lease), and resolving non-fast-forward rejections.
Configuring .gitignore
How .gitignore works: glob patterns, ignoring node_modules and .env secrets, untracking previously committed files, and global gitignore.