Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Inspecting History

git reflog command

Reference for git reflog: show, expire, delete and exists. Use the reflog to recover lost commits after a reset, rebase, amend or deleted branch.

The reflog is a local journal of where each ref (HEAD, every branch) pointed over time. Every time a ref moves because of a commit, checkout, reset, merge, rebase or pull, Git appends a line with the old and new value and a short description. git reflog reads that journal.

It is your safety net: a commit that no branch points to any more is not gone, it is only unreachable, and the reflog still tells you its hash. Reflogs are local: they are never pushed or cloned, and they expire (by default 90 days for reachable entries and 30 days for unreachable ones).

Terminal
git reflog [show] [<log-options>] [<ref>]
git reflog expire [--expire=<time>] [--all]
git reflog delete <ref>@{<n>}
git reflog exists <ref>

Try it

Bash Recover commits lost to a hard reset

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash A deleted branch and per-branch reflogs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

git reflog with no subcommand is git reflog show, which is an alias for git log -g --abbrev-commit --pretty=oneline. Any git log option works, so --format, --date=iso and -n are all useful. The default ref is HEAD.

Subcommand / option Description
show [<ref>] Print entries, newest first, labeled HEAD@{0}, HEAD@{1}, and so on.
expire Remove old entries. --expire=<time> for reachable, --expire-unreachable=<time> for others, --all for every ref, --stale-fix to drop entries pointing to missing objects.
delete <ref>@{n} Remove one entry. --rewrite and --updateref adjust neighbors.
exists <ref> Exit status 0 if the ref has a reflog, 1 otherwise.
--date=<format> Show dates instead of indexes: HEAD@{2 weeks ago} style.
--all Show the reflogs of all refs together.

Useful format placeholders: %gd (reflog selector), %gs (reflog subject, such as commit: Add three or reset: moving to HEAD~2), %gn and %ge (reflog identity).

Usage notes

  • HEAD@{n} counts moves, not commits. HEAD@{1} means "where HEAD was before its last move". main@{yesterday} and main@{2.days.ago} use time instead; see reflog names.
  • A deleted branch has no reflog of its own any more, but HEAD's reflog still lists the commits you were on. Recreate it with git branch idea <hash>.
  • git reset --hard, git rebase, git commit --amend and git branch -D are all recoverable this way, as long as the entries have not expired and git gc has not pruned the objects.
  • Uncommitted changes destroyed by reset --hard or checkout are not in the reflog; only committed states are.
  • Bare repositories have no reflogs by default (core.logAllRefUpdates is off there); a clone made with --bare loses this safety net.
  • git reflog expire --expire=now --all followed by git gc --prune=now makes the old commits really disappear; do that only on purpose, for example after removing a secret.
  • Expiry times are configurable: gc.reflogExpire and gc.reflogExpireUnreachable.