LinuxCommandLibrary

git-reflog

Recover lost commits or branch states

TLDR

Show the reflog for HEAD

$ git reflog
copy

Show the reflog for a given branch
$ git reflog [branch_name]
copy

Show only the 5 latest entries in the reflog
$ git reflog [[-n|--max-count]] 5
copy

SYNOPSIS

git reflog [options] [ref] [--] [pattern]

PARAMETERS

--all
    Show reflog for all references

--branches
    Show reflog for all branches

--tags
    Show reflog for all tags

--upto=<time-spec>
    Show reflogs created up to time-spec

--since=<time-spec>
    Show reflogs newer than time-spec

--until=<time-spec>
    Show reflogs older than time-spec

--after=<time-spec>
    Synonym for --since

--before=<time-spec>
    Synonym for --until

-p, --pretty[=<format>]
    Show patch with each entry

--grep=<pattern>
    Limit to reflog entries matching pattern

--invert-grep
    Invert grep matches

--format=<format>
    Format output (like git-log)

--date=<format>
    Specify date format

--no-decorate
    Omit ref names in output

--decorate[=<options>]
    Add ref names to output

--color[=<when>]
    Enable colored output

--no-color
    Disable colored output

DESCRIPTION

The git reflog command shows a detailed log of all updates to Git references, such as HEAD and branch tips. Unlike git log, which traces commit ancestry, reflog records every ref movement including commits, checkouts, merges, rebases, amends, and resets—even to dangling commits not reachable from any branch.

This history is crucial for data recovery after accidental operations like git reset --hard or failed rebases. Each entry lists the previous ref value, new value, timestamp, and action message (e.g., 'commit', 'reset'). By default, it displays HEAD's reflog, limited to recent entries.

Reflogs are stored in .git/logs/ per ref and expire automatically: 90 days for referenced refs, 30 days for unreferenced (configurable via gc.reflogExpire). Use git reflog --all for all refs or filter with time options like --since. Output mimics git log format, supporting --pretty for diffs.

Pro tip: Pipe to grep for SHAs, then create a branch from lost commits before they prune.

CAVEATS

Reflogs expire via git gc (default 90/30 days); unreachable commits become unrecoverable. Not shared in bare repos or clones. Local to each clone.

RECOVERY WORKFLOW

1. Run git reflog to find lost SHA.
2. git checkout -b recovery SHA or git cherry-pick SHA.
3. Prune with git reflog expire --expire=now --all && git gc --prune=now.

STORAGE LOCATION

Per-ref logs in .git/logs/{heads|refs}/; HEAD in .git/logs/HEAD. View raw with cat .git/logs/HEAD.

HISTORY

Introduced in Git 1.0.4 (April 2005) by Junio C. Hamano to aid recovery from ref manipulations. Evolved with Git's plumbing; reflog plumbing added in 1.5.0 (2007). Widely used since for bisecting errors and salvaging work.

SEE ALSO

git log(1), git reflog expire(1), git fsck(1), git gc(1), git show-ref(1)

Copied to clipboard