← Back to list

Git Reflog : Bringing Lost Data Back

git reflog records every movement of the HEAD in your local repository.

Piyali Das · 2026-07-14 12:42 · 0 claps · 2.0 min read
#git #programming #technology #coding #ui
Open on Medium ↗
Wiki topics: 💻 · Programming 🔓 · Open Source

Git Reflog : Bringing Lost Data Back

git reflog records every movement of the HEAD in your local repository.

Unlike git log, which only shows commits that are reachable, git reflog keeps a history of where HEAD has pointed, making it possible to recover commits that seem "lost."

Git Log
Shows reachable commits

Git Reflog
Shows every HEAD movement

Why use Git Reflog?

Suppose you accidentally execute:

git reset --hard HEAD~1

Your latest commit disappears.

Without git reflog, you might think it's gone forever.

With git reflog, you can recover it.

Example

Initial history

A ---- B ---- C (HEAD)

You accidentally run

git reset --hard HEAD~1

Now

A ---- B (HEAD)

C appears lost

However,

git reflog

might show

3a5f72 HEAD@{0}: reset: moving to HEAD~1
8fdc21 HEAD@{1}: commit: File3 added

Notice that Git still remembers commit C.

Recover the Lost Commit

Copy the commit hash.

Then run

git reset --hard 8fdc21

Now history becomes

A ---- B ---- C (HEAD)

The commit is restored.

Recover a Deleted Branch

Suppose

master

A ---- B

feature

A ---- B ---- C

Delete the branch

git branch -D feature

Now

master

A ---- B

feature ❌ deleted

The commit still exists temporarily.

Find it

git reflog

Example

7d13ef HEAD@{3}: commit: File3 added

Checkout that commit

git checkout 7d13ef

or

git switch --detach 7d13ef

You’re now in detached HEAD state.

Create the branch again

git switch -c feature

Now

master

A ---- B

feature

A ---- B ---- C

Branch recovered.

Detached HEAD

When checking out a commit directly

git checkout <commit_hash>

or

git switch --detach <commit_hash>

you are not on any branch.

master
   |
A ---- B ---- C

          ^
         HEAD

Create a branch if you want to keep new work

git switch -c new-feature

Git Reflog vs Git Log

| Git Log                      | Git Reflog                   |
| ---------------------------- | ---------------------------- |
| Shows commit history         | Shows HEAD movement history  |
| Shared with others           | Local only                   |
| Reachable commits only       | Includes unreachable commits |
| Used to view project history | Used to recover lost work    |

Common Commands

View reflog

git reflog

Recover a commit

git reset --hard <commit_hash>

Checkout a lost commit

git checkout <commit_hash>

Create a branch from that commit

git switch -c feature

Reflog Retention

git reflog does not keep entries forever.

Typically:

  • Reachable reflog entries are retained for about 90 days.
  • Unreachable entries (such as after deleting commits or branches) are typically retained for about 30 days, after which Git’s garbage collection may permanently remove them.

Real-World Example

You’re working on a feature:

Login
Dashboard
Payment

You accidentally run

git reset --hard HEAD~2

The last two commits disappear.

Instead of panicking:

git reflog

Find

HEAD@{3}: commit: Payment completed

Recover it

git reset --hard HEAD@{3}

or

git reset --hard <commit_hash>

Your work is back.

Interview Tip

Q: What is the difference between git log and git reflog?

Answer: git log displays the commit history that is currently reachable from a branch, while git reflog records every movement of the local HEAD, including commits that are no longer referenced due to actions like git reset, branch deletion, or rebasing. Because of this, git reflog is commonly used to recover accidentally lost commits or deleted branches.


메타데이터
post_id
640a9d6ab1ec
slug
git-reflog-bringing-lost-data-back-640a9d6ab1ec
url
https://medium.com/@piyalidas.it/git-reflog-bringing-lost-data-back-640a9d6ab1ec
canonical_url
https://medium.com/@piyalidas.it/git-reflog-bringing-lost-data-back-640a9d6ab1ec
author_url
https://medium.com/@piyalidas.it
status
ok
fetched_at
2026-07-15 14:13:45