linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
Intro
Lessons
Footer
linuxlab-TutorialsPricingAboutPrivacy & cookies
Copyright © 2026 LinuxLab. All rights reserved.
linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
  • Introduction
  • Chapters
  • How it works
  • Lessons
  • Knowledge base
  • Interview prep
home/git/kb/Tools/reflog

kb/tools ── Tools ── intermediate

git reflog

A log of all HEAD movements on this machine. By default, entries for reachable commits live for 90 days; entries for unreachable commits live for 30. The primary recovery tool after `reset --hard`, `--amend`, `rebase`, and branch deletion. Stored locally in `.git/logs/`.

view as markdownaka: git-reflog

Reflog answers "where was HEAD five minutes ago," which is why it lets you recover almost any state you have been in recently.

Basic form

bash
git reflog
# a1b2c3d HEAD@{0}: reset: moving to HEAD~3
# 4d5e6f7 HEAD@{1}: commit: feat: add login
# 8a9b0c1 HEAD@{2}: commit: docs: update README
# ...

Each line is the state of HEAD at the moment of an operation. Newer entries appear at the top. Columns: SHA, syntactic ref, reason.

The most common scenario

After an accidental reset:

bash
git reset --hard HEAD~10    # oops, wrong target
git reflog                  # first line shows the SHA after the reset,
                            # second line shows the SHA before it
git reset --hard HEAD@{1}   # return to the state before the reset

Or directly by SHA:

bash
git reset --hard 4d5e6f7    # SHA from reflog

What reflog covers

Reflog records nearly every operation that moves HEAD:

  • commit, commit --amend
  • checkout/switch between branches
  • merge, rebase (a whole series of entries)
  • reset --hard, reset --soft
  • pull (moves HEAD via its merge/rebase; a plain fetch does not touch HEAD, it only updates refs/remotes/*, which have their own separate reflog)
  • Branch creation/deletion via branch -d/-D (the last position of that branch)

Each branch also has its own reflog:

bash
git reflog show feature        # history of positions for branch feature
git reflog show HEAD           # equivalent to git reflog

When reflog will not help

  • Uncommitted changes. If a file was only in the working tree or the index and was never committed, reflog cannot see it. Reflog tracks commits only.
  • Untracked files deleted with git clean -fd. Same case.
  • Other machines. Reflog is local. Your colleague has their own reflog; your actions are not in it.
  • Older than the reflog retention period. The default is 90 days for reachable objects and 30 days for unreachable ones. After that, git gc cleans them up.
  • After an explicit git gc --prune=now + git reflog expire. These commands erase history immediately.

Adjusting retention periods

If you work with very long-lived branches or want a longer safety net:

bash
git config --global gc.reflogExpire "180 days"
git config --global gc.reflogExpireUnreachable "90 days"

Or to disable reflog entirely (rarely needed, saves disk space):

bash
git config --global gc.reflogExpire 0
git config --global gc.reflogExpireUnreachable 0

Pitfalls

  • HEAD@{N} is not HEAD~N. The first is the N-th entry in the reflog (by time). The second is the N-th ancestor commit (by graph). They are different things.
  • HEAD@{yesterday}, HEAD@{2.hours.ago} are time-based syntax. Handy: git diff HEAD@{1.week.ago} HEAD.
  • Reflog is stored in .git/logs/ as plain text files. You can read them directly, but git reflog is more convenient.
  • On a freshly cloned repository the reflog is empty. History lives in the commits, not in the reflog. Reflog records only local HEAD movements, and a new clone has none yet.

§ команды

bash
git reflog

Log of HEAD movements (90 days for reachable, 30 for unreachable by default)

bash
git reset --hard HEAD@{1}

Return to the previous HEAD state

bash
git reflog show <branch>

History of positions for a specific branch

bash
git diff HEAD@{1.week.ago} HEAD

What changed locally over the past week

§ см. также

  • stashgit stashSaves uncommitted changes to a special stack and cleans the working tree back to HEAD. Useful when you need to switch branches urgently but are not ready to commit.
  • amendgit commit --amendRewrites the last commit: changes the message, folds in forgotten edits, or updates the content. Creates a new object with a new SHA. Safe before push; dangerous after.
Footer
linuxlab-
Copyright © 2026 LinuxLab. All rights reserved.
Tutorials
Pricing
About
Privacy & cookies