Sub topic 4
HEAD reflog and branch reflogs
This chapter assumes you can commit, branch, and switch branches, and that you know a branch is a pointer to a commit. It also assumes you have seen basic git reflog output. Here you will learn that Git keeps more than one reflog, and why that matters when you hunt for lost work.
One reflog per reference
Git does not keep a single reflog. It keeps one log for each reference that moves: one for HEAD, and one for every local branch. Each log is a separate file under .git/logs/.
ls .git/logs
ls .git/logs/refs/headsThe first command shows a file named HEAD and a refs directory. The second lists one file per local branch, such as main or feature.
Running git reflog with no arguments shows the HEAD reflog. It is the same as git reflog show HEAD.
What each log records
The two kinds of log record different events.
| Log | Records | Typical entries |
|---|---|---|
| HEAD reflog | Every change to where `HEAD` points, whichever branch you are on | commit, checkout, reset, rebase, merge, pull |
| Branch reflog | Every change to that one branch tip | commit, reset, rebase, merge, branch creation |
The key difference is switching. When you switch branches, HEAD moves but neither branch tip does. So a checkout: moving from feature to main entry appears only in the HEAD reflog.
Comparing the two views
Show the log for a single branch by naming it:
git reflog show main --date=relative
git reflog show HEAD --date=relativeThe main log lists only what happened to main. The HEAD log lists everything you did in the working tree, in order, across all branches. That makes it the better place to reconstruct a session.
The diagram below shows how one sequence of commands feeds the two logs.
Which log to search
Choose the log by what you lost.
- Use the
HEADreflog when you do not remember which branch you were on, when you reset or rebased, or when you worked on a detachedHEAD. It also holds thecheckoutentries that show where a deleted branch last pointed. - Use a branch reflog when you want the history of one branch only, with no switching noise. It is shorter and easier to read.
A deleted branch loses its own reflog. Git removes .git/logs/refs/heads/<name> along with the branch. The HEAD reflog is then the only place that still mentions its commits.
Reading entries from a branch log
Entries in a branch reflog use the selector form branch@{n}. The numbering is per log, so main@{1} and HEAD@{1} often name different commits.
git rev-parse main@{1}
git rev-parse HEAD@{1}If you have just switched branches, HEAD@{1} is the commit you were on before the switch. main@{1} is the previous tip of main, which may not have changed at all.
Exercise
You deleted the branch feature and now want its last commit. Which reflog should you search, and why can't you use git reflog show feature?
Show answer
Search the HEAD reflog (git reflog). Deleting a branch also removes that branch's own reflog, so git reflog show feature has nothing to show. The HEAD reflog still holds the commits and the checkout: moving from feature entries.