Sub topic 11
How long reflog entries last
This chapter assumes you can commit, branch, and read git reflog output. It covers when Git deletes reflog entries and how to check or change those limits.
Why entries expire
The reflog is a local log, and Git does not keep it forever. Each entry also keeps the commit it names from being garbage collected. If entries never expired, the repository would never free the space used by abandoned commits.
Git removes old entries with git reflog expire. You rarely run it yourself. git gc runs it, and Git sometimes starts git gc in the background after commands such as git commit or git fetch.
The two default limits
Two settings control expiry. Each has a default:
| Setting | Default | Applies to |
|---|---|---|
| `gc.reflogExpire` | 90 days | Entries whose commit is still reachable from the current ref tip |
| `gc.reflogExpireUnreachable` | 30 days | Entries whose commit is no longer reachable from the ref tip |
Unreachable entries are the ones you care about during recovery. A commit you discarded with git reset --hard is unreachable from the branch. Its reflog entry therefore lasts about 30 days, not 90.
The age is counted from the time the entry was written, not from the time of the commit. A commit you made a year ago but checked out yesterday has an entry that is one day old.
Checking your settings
Ask Git for the effective values:
git config --get gc.reflogExpire
git config --get gc.reflogExpireUnreachableNo output means the setting is not configured, so Git uses the default. You can also look at the age of entries directly:
git reflog --date=relativeEach selector shows how long ago the move happened, such as HEAD@{3 weeks ago}. This tells you how close the entry is to the limit.
Changing the limits
To keep entries longer in one repository, set larger values:
git config gc.reflogExpire "180 days"
git config gc.reflogExpireUnreachable "90 days"Use --global to apply the change to every repository for your user. The value never turns expiry off. Doing so makes the repository grow, because every abandoned commit stays on disk.
Expiry is not the same as deletion
Two steps must both happen before a commit is gone:
- The reflog entry expires, so nothing in the reflog points to the commit.
- Garbage collection prunes the commit. Unreachable objects are removed only if they are older than
gc.pruneExpire, which is two weeks by default.
If the reflog entry is gone but the object has not been pruned, you can still find the commit with git fsck --lost-found, which a later chapter covers.
The sequence below shows the order of events for a commit you discard with a hard reset.
Common mistakes
- Running
git gc --prune=noworgit reflog expire --expire=now --allwhile experimenting. Both remove the safety net at once. Use them only when you want the data gone. - Assuming a fresh clone has your history. A clone starts with an empty reflog. The old entries stay in the original repository.
- Waiting too long. Recover within days, not weeks. Entries are still there until a
gcrun removes them, but you cannot know when that run will happen.
Summary of the working model
- Reflog entries for commits that are still reachable last 90 days by default.
- Entries for unreachable commits last 30 days by default.
- Expiry happens only when
git gcorgit reflog expireruns. - A commit survives past its entry until it is pruned.
Exercise
You ran git reset --hard HEAD~3 40 days ago and never ran git gc by hand. With default settings, can you rely on git reflog to find the discarded commits? Explain.
Show answer
No, you cannot rely on it. The discarded commits became unreachable, so their entries last 30 days by default (gc.reflogExpireUnreachable). At 40 days the entries are past that limit, and any git gc run, including one Git starts automatically, may have removed them. If the entries are gone, try git fsck --lost-found, but the commit objects may also have been pruned.
Exercise
Which command makes Git keep unreachable reflog entries for 60 days in the current repository only?
Show answer
git config gc.reflogExpireUnreachable "60 days". Leaving out --global applies the setting to this repository only.