AI Hints
Home›Git & GitHub Tutorial›Safety Nets
Git & GitHub · Lesson 33 of 144

The Safety Net

Git reflog.

⏱ 2 min read▶ Video at 35:11🎓 Free
Watch this lesson in the Git & GitHub courseWatch on YouTube ↗Full Git & GitHub course

You ran reset dash dash hard, and lost a commit you actually needed. Is it gone forever? Almost never. Git keeps a private diary of everywhere head has been. It's called the reflog.

The Safety Net
From the video: The Safety Net
  • Oops, reset --hard
  • Is the commit gone?
  • Almost never: the reflog

Losing a commit

First, the accident. We reset hard, one commit back, and the log no longer shows the commit called add sale banner. It looks gone.

git
git reset --hard HEAD~1
HEAD is now at f63a17a Link to about page
git log --oneline -n 2
f63a17a (HEAD -> main) Link to about page
ef9c947 Add basic styles

Finding it again

Now git reflog. Every line is a place head has been. The second line is where we were before the reset, the add sale banner commit, nine five c seven f eight d. Reset hard to that hash, and the log shows the commit is back, as if nothing happened.

git
git reflog -n 3
f63a17a (HEAD -> main) HEAD@{0}: reset: moving to HEAD~1
95c7f8d HEAD@{1}: commit: Add sale banner
f63a17a (HEAD -> main) HEAD@{2}: commit: Link to about page
git reset --hard 95c7f8d
HEAD is now at 95c7f8d Add sale banner
git log --oneline -n 2
95c7f8d (HEAD -> main) Add sale banner
f63a17a Link to about page

What the reflog keeps

The reflog records every commit, reset, switch and merge, on your computer. Entries are kept for about ninety days. It's only on your machine, so it isn't shared. And it can't save edits you never committed. That's one more reason to commit often.

Recordsevery move of HEAD
Keptabout 90 days
Localonly your computer
Can't saveuncommitted edits

Summary

git reflog shows everywhere head has been, so you can find lost commits and reset back to them. Next, telling Git which files to ignore.

Key takeaways
  • git reflog
  • Find the lost hash
  • git reset --hard <hash>
Next up: Ignoring Files — .gitignore