Sub topic 2
What the reflog is
This chapter assumes you can commit, branch, switch branches, and merge. It also assumes you know a branch is a movable pointer to a commit. If those are new, review them first.
The problem the reflog solves
A branch name points at one commit. When you run git reset --hard, delete a branch, or rebase, that pointer moves. The old commits are still in the repository's object database, but nothing points at them any more. Without a record of where the pointer used to be, you would need to remember the commit hash.
The reflog is that record. It is a local log of every position that HEAD and each branch tip have held, in order. Git writes an entry each time one of them moves because of a commit, checkout, reset, merge, rebase, amend, or similar action.
What the reflog stores
Each entry holds the new commit hash, a position number, and a short description of the action that caused the move. Here is the output for a repository where two commits were just discarded:
git reflog
# 3f2a9c1 HEAD@{0}: reset: moving to HEAD~2
# 9d4e7b8 HEAD@{1}: commit: Add export feature
# c81b0aa HEAD@{2}: commit: Fix parser bug
# 5e6f1d2 HEAD@{3}: commit: Start parser workThe newest entry is at the top. HEAD@{0} is where HEAD is now, and HEAD@{1} is where it was one move ago. The commit 9d4e7b8 no longer appears in git log, but the reflog still names it. That hash is your way back.
Why the reflog is local
The reflog lives in your own repository, under .git/logs/. It is not part of the commit history, and it is never sent by git push or fetched by git clone. Three consequences follow:
- A fresh clone has almost no reflog. It only records moves made in that clone.
- A teammate cannot see your reflog, and you cannot see theirs.
- Recovery only works in the repository where the work was lost.
Reflog versus log
Both commands show commits, but they answer different questions.
| Command | What it shows | Shared with others |
|---|---|---|
| `git log` | Commits reachable from a ref by following parent links | Yes, it is the project history |
| `git reflog` | Where `HEAD` or a branch pointed over time | No, it is local only |
git log shows what the history is. git reflog shows what your pointers did. A commit that was reset away is absent from the first and present in the second.
What the reflog does not do
The reflog is not a backup system. It records commits that existed. Changes that were never committed are not in it, so a git reset --hard that wipes uncommitted edits cannot be undone this way. Entries also expire after a set time, which a later chapter covers.
Check your understanding
Exercise
You run git reset --hard HEAD~3 and git log no longer shows your last three commits. Why can the reflog still help, and why would it not help a teammate on another machine?
Show answer
The reset only moved the branch pointer. The commits remain in the object database, and your local reflog has an entry recording the hash the branch had before the reset. The reflog is stored only in your own repository's .git/logs/ and is never pushed or cloned, so a teammate's repository has no such entry.
Exercise
In the output of git reflog, which entry is the newest, and what does HEAD@{1} mean?
Show answer
The newest entry is at the top and is HEAD@{0}. HEAD@{1} is where HEAD pointed one move before the current position.