What is git reflog and how can it help?

The reflog records every move of HEAD and branch tips in your local repository. Even if a commit is not in any branch anymore, you can find it there.

Terminal output

$ git reflog
0057196 (HEAD -> main) HEAD@{0}: reset: moving to HEAD~1
f411fbd HEAD@{1}: commit: Second
0057196 (HEAD -> main) HEAD@{2}: commit (initial): First
$ git reset –hard HEAD@{1}
HEAD is now at f411fbd Second
$ git log –oneline
f411fbd (HEAD -> main) Second
0057196 First

The reflog is local only, and entries expire after some time (90 days by default). See Git Reflog.


Learn more about the similar topics:
Tutorials
Git Restore
Git Commit Amend
Git Reset
Git Revert
Git Stash
Git Reflog
Exercises & Assignments
Discard the changes of a file
Undo the last commit but keep the changes
Revert a commit with git revert
Fix the message of the last commit
Stash your changes and bring them back
Recover a deleted branch with git reflog
Interview Questions & Answers
What is git revert?
How to undo last commit using git reset?
What is the difference between git reset and git revert?
What is the difference between soft, mixed and hard reset in Git?
What is git stash and when do you use it?
How do you change the last commit message in Git?
How do you squash commits in Git?