World map

 

BM: Baca dalam Bahasa Melayu

World 2: Undo Magic

Reflog, your safety net

You will learn: how to find and bring back "lost" commits.

The problem

main and HEAD jump back one commit. Add day 3 entry turns into a dashed, lost dot.
6d3a9b1a8feb63270a76ebcf4670031ae57

Oops. You typed git reset --hard HEAD~1. Your day 3 entry vanished. Panic?

Watch

git reflog lists every move of HEAD, newest first. The second line says: commit: Add day 3 entry. Terminal output: $ git reflog bcf4670 (HEAD -> main) HEAD@{0}: reset: moving to HEAD~1 031ae57 HEAD@{1}: commit: Add day 3 entry bcf4670 (HEAD -> main) HEAD@{2}: commit: Add day 2 entry 270a76e HEAD@{3}: commit: Add style a8feb63 HEAD@{4}: commit: Add day 1 page 6d3a9b1 HEAD@{5}: commit (initial): Add README

No panic. Git keeps a diary of every place HEAD has been. This is the reflog: Git's diary of everywhere HEAD has been.. Your lost commit is still listed.

Try it

Find your lost commit in the reflog.

Tap the line with Add day 3 entry.

~/diary $ git reflog

0 of 1 found

Watch

The two newest reflog lines: HEAD@{0} is the reset, HEAD@{1} is the Add day 3 entry commit. Terminal output: $ git reflog -n 2 bcf4670 (HEAD -> main) HEAD@{0}: reset: moving to HEAD~1 031ae57 HEAD@{1}: commit: Add day 3 entry

HEAD@{1} means “where HEAD was one move ago”. You can use that name in commands.

Try it

Bring the day 3 commit back.

6d3a9b1a8feb63270a76ebcf4670031ae57

Predict

What can the reflog bring back?

Pick one answer.

You did it!

You can now bring back “lost” commits with the reflog.

Go deeper (optional)

The reflog lives only on your computer. It is never pushed. Old lines expire, after 90 days by default. Lines for lost commits go after 30. You can also reset to a hash from the reflog list.

After git reset --hard HEAD@{1}, git log shows Add day 3 entry on top again. Terminal output: $ git reset --hard HEAD@{1} HEAD is now at 031ae57 Add day 3 entry $ git log --oneline 031ae57 (HEAD -> main) Add day 3 entry bcf4670 Add day 2 entry 270a76e Add style a8feb63 Add day 1 page 6d3a9b1 Add README