Skip to content

Git Reflog Explained: Find and Recover Lost Commits

86 people viewed this

Git Reflog Explained: Find and Recover Lost Commits

Git Reflog Explained — Find and Recover Lost Commits

Have you ever accidentally run git reset --hard, deleted a branch, or performed a rebase and then realized that some important work disappeared?

Don't panic.

Git has a powerful recovery mechanism called Reflog.

In this article, we will learn git reflog from beginner to practical level, including how to recover lost commits and deleted branches with PHP and Laravel examples.


1. What Is Git Reflog?

Git Reflog is a record of where references such as HEAD and branch references have pointed in your local repository.

In simple words:

Git log shows the commit history. Git reflog shows the history of where HEAD and references have moved.

For example, you might have this normal history:

A → B → C → D

Then accidentally run:

git reset --hard B

Now your branch may look like:

A → B

Commits C and D may no longer appear in the normal branch history.

But Git Reflog may still remember that HEAD previously pointed to D.

git reflog

You can use that information to recover the lost work.


2. Git Log vs Git Reflog

This is one of the most important differences to understand.

Git Log Git Reflog
Shows reachable commit history Shows reference/HEAD movements
Useful for understanding project history Useful for recovering lost work
Usually shared conceptually with the repository history Primarily local
Does not normally show unreachable commits Can help locate commits that are no longer reachable from a branch
Used for history inspection Used heavily for recovery and troubleshooting

For normal history:

git log --oneline

For reference movement:

git reflog

3. Basic Git Reflog Command

The simplest command is:

git reflog

You may see something like:

a82d921 HEAD@{0}: commit: Add payment service
b91c441 HEAD@{1}: commit: Add payment validation
c72d123 HEAD@{2}: checkout: moving from develop to feature/payment
d31b8c4 HEAD@{3}: commit: Add payment controller
f50d3a1 HEAD@{4}: checkout: moving from main to develop

Each entry tells you where HEAD was and what operation caused the movement.


4. Understanding HEAD@{0}, HEAD@{1}, etc.

The values such as HEAD@{0} represent positions in the reflog.

HEAD@{0}

means the current/latest reflog position.

HEAD@{1}

means the previous reflog position.

HEAD@{2}

means an older position.

For example:

HEAD@{0} → current position
HEAD@{1} → previous position
HEAD@{2} → older position
HEAD@{3} → even older position

These references are especially useful when recovering from accidental Git operations.


5. What Does Reflog Record?

Reflog can record reference movements caused by operations such as:

  • Commit
  • Checkout
  • Reset
  • Rebase
  • Merge
  • Pull
  • Branch movement
  • Other reference updates

For example:

git reflog

a82d921 HEAD@{0}: commit: Add payment API
b91c441 HEAD@{1}: reset: moving to HEAD~2
c72d123 HEAD@{2}: commit: Add payment validation
d31b8c4 HEAD@{3}: commit: Add payment controller

This can be extremely useful when something goes wrong.


6. Why Is Git Reflog Important?

Git Reflog can help recover work after accidental operations.

Common situations include:

  • Accidental git reset --hard
  • Deleted local branch
  • Incorrect rebase
  • Accidental checkout
  • Lost commit after reset
  • Recovering a previous branch position
  • Finding a commit that disappeared from git log

7. Practical Example — Accidental git reset --hard

Imagine you are working on a Laravel application.

Your history is:

A → B → C → D

Commit D contains an important payment feature.

You accidentally run:

git reset --hard HEAD~2

Now your branch points to B:

A → B

When you run:

git log --oneline

commits C and D may no longer appear in the current branch history.

This is where Reflog helps.

git reflog

You might find:

b91c441 HEAD@{0}: reset: moving to HEAD~2
a82d921 HEAD@{1}: commit: Add payment feature
c72d123 HEAD@{2}: commit: Add payment validation

The old commit is still identifiable.


8. Recover a Lost Commit

Suppose the lost commit is:

a82d921

You can inspect it first:

git show a82d921

If it is the commit you want to recover, you can move the branch back to it:

git reset --hard a82d921

Or, if you don't want to move your current branch and simply want the changes from that commit, you can create a new branch from it:

git switch -c recovery/payment a82d921

This is often a safer way to inspect and recover the work first.


9. Safer Recovery Workflow

When you realize something has gone wrong, don't immediately run another destructive command.

Use this workflow:

git status

git reflog

git show <commit-hash>

git switch -c recovery-branch <commit-hash>

Now you have a recovery branch that points to the commit.

You can inspect the code and decide what to do next.


10. Recover a Deleted Local Branch

One of the most useful Reflog scenarios is recovering a deleted local branch.

Suppose you had:

feature/pharmacy-payment

and accidentally deleted it:

git branch -D feature/pharmacy-payment

The branch reference is gone.

But the commits may still be recoverable through Reflog.

Run:

git reflog

Find the commit where the branch was last pointing.

For example:

a82d921 HEAD@{5}: commit: Add pharmacy payment API

Create the branch again:

git switch -c feature/pharmacy-payment a82d921

The branch is restored at that commit.


11. Recover After an Incorrect Rebase

Rebase rewrites commit history. Sometimes a developer performs a rebase and realizes afterward that the result is incorrect.

For example:

git rebase main

After the rebase, something looks wrong.

Instead of guessing which commits were moved, inspect:

git reflog

You may find entries such as:

f81a123 HEAD@{0}: rebase (finish): returning to refs/heads/feature/payment
a82d921 HEAD@{1}: rebase (pick): Add payment validation
c72d123 HEAD@{2}: rebase (start): checkout main
b91c441 HEAD@{3}: checkout: moving from main to feature/payment

The reflog can help you identify where the branch was positioned before the rebase.

You can then create a temporary recovery branch from the appropriate commit:

git switch -c recovery-before-rebase b91c441

12. Reflog After git reset

Consider:

git reset --hard HEAD~3

If you realize this was a mistake, immediately check:

git reflog

You may see:

a82d921 HEAD@{0}: reset: moving to HEAD~3
f91b821 HEAD@{1}: commit: Add payment tests
e82c731 HEAD@{2}: commit: Add payment service
d72a112 HEAD@{3}: commit: Add payment controller

The previous HEAD position can be identified from the reflog.

You can inspect it:

git show HEAD@{1}

Then create a recovery branch if needed:

git switch -c recovery/payment HEAD@{1}

13. Using HEAD@{n} Directly

You don't always need to copy the commit hash. Git allows you to use the reflog reference directly.

git show HEAD@{1}

Or:

git switch -c recovery HEAD@{3}

You can also reset to a previous position when you are certain that is what you want:

git reset --hard HEAD@{2}

Be careful with --hard because it changes the working tree and can discard uncommitted changes.


14. Git Reflog for a Specific Branch

You can inspect the reflog for a particular branch.

git reflog show feature/payment

You can also use:

git reflog show main

This is useful when investigating how a particular branch moved.


15. Useful Reflog Commands

View HEAD reflog

git reflog

View more entries

git reflog --all

View a specific branch

git reflog show main

Show a previous HEAD position

git show HEAD@{2}

Create recovery branch

git switch -c recovery HEAD@{2}

View reflog with relative time

git reflog --date=relative

You may see:

a82d921 HEAD@{5 minutes ago}: commit: Add payment API
b91c441 HEAD@{8 minutes ago}: reset: moving to HEAD~2
c72d123 HEAD@{10 minutes ago}: commit: Add payment validation

16. Git Reflog and Laravel Development

Imagine you are working on a Laravel pharmacy ERP application.

You have completed:

feat(payment): add payment service
feat(payment): integrate payment gateway
test(payment): add payment tests

Then you accidentally execute:

git reset --hard HEAD~3

Your payment work appears to be gone.

Don't start rewriting the code from memory.

First:

git reflog

Find the previous HEAD.

a82d921 HEAD@{1}: commit: test(payment): add payment tests

Inspect it:

git show a82d921

Create a recovery branch:

git switch -c recovery/payment a82d921

Now you can verify the Laravel files and run:

php artisan test

Once you confirm the recovery is correct, you can decide whether to merge or otherwise restore the work into your original branch.


17. Reflog Is Mainly a Local Recovery Tool

An important limitation is that Reflog is not a replacement for a remote repository or backup.

Reflog records local reference movements. It is not something you should assume another developer can inspect from their machine.

For example, if your local machine contains:

git reflog

another developer does not automatically have access to your local reflog.

This is why important work should still be pushed to an appropriate remote branch according to your team's workflow.


18. Does git reflog Work on Remote Branches?

Reflog primarily tracks local reference movements. Whether a remote-tracking reference has a reflog depends on repository configuration and how the reference is maintained locally.

Do not treat your local reflog as a permanent record of everything that has happened on a remote repository.

For shared work, use your remote repository, such as Bitbucket, as part of the normal backup and collaboration workflow.


19. Reflog Does Not Mean Deleted Data Lives Forever

This is an important point.

Reflog can help recover recently lost commits, but it is not a permanent backup.

Git eventually removes unreachable objects according to its garbage-collection and expiration rules.

Therefore:

If you accidentally lose important work, investigate and recover it as soon as possible.

20. Git Reflog vs Git Reset

Command Purpose
git reset Moves a branch/HEAD to another commit
git reflog Shows previous reference positions

A useful way to remember them is:

git reset  → changes where your branch points

git reflog → helps you find where it pointed before

21. Reflog vs Log

git log
    ↓
"What commits are reachable from my current history?"

git reflog
    ↓
"Where has HEAD or this reference been recently?"

That distinction is extremely useful during Git troubleshooting.


22. A Real Recovery Scenario

Imagine your branch contains:

A → B → C → D → E

You decide to remove the last two commits and run:

git reset --hard HEAD~2

Now:

A → B → C

Later you realize that D contained an important Laravel feature.

Run:

git reflog

You may see:

c123456 HEAD@{0}: reset: moving to HEAD~2
d123456 HEAD@{1}: commit: Add payment feature
e123456 HEAD@{2}: commit: Add payment tests

Recover D by creating a branch:

git switch -c recovery/payment d123456

Now your work is accessible again.


23. Recovering Multiple Lost Commits

Suppose you accidentally reset past three commits:

A → B → C → D → E

and your branch moves back to B.

The reflog may identify E as the previous branch position.

If E contains the complete desired state, creating a branch at E can recover the entire line of work:

git switch -c recovery/feature E

You can then inspect the recovered branch before deciding how to restore it into your main development branch.


24. Reflog After git commit --amend

Suppose you create a commit:

git commit -m "feat(auth): add login"

Then amend it:

git commit --amend -m "feat(auth): add login validation"

The commit ID changes because Git created a new commit.

The previous position can often still be found in the reflog:

git reflog

This is another example of why Reflog is useful when investigating local history changes.


25. Reflog After Checkout

When you switch branches, HEAD moves.

git switch main
git switch feature/payment

Reflog records these movements.

You may see:

a82d921 HEAD@{0}: checkout: moving from main to feature/payment
b91c441 HEAD@{1}: checkout: moving from feature/payment to main

This can help you understand where HEAD was during a troubleshooting session.


26. Best Practices When Using Git Reflog

  • Run git reflog before attempting complicated recovery.
  • Inspect the suspected commit with git show.
  • Create a temporary recovery branch before making another destructive change.
  • Do not blindly use git reset --hard.
  • Push important work to the remote repository.
  • Do not assume reflog is a permanent backup.
  • Be careful when rewriting shared history.
  • Verify recovered Laravel code with your test suite.

27. Common Mistakes With Git Reflog

❌ Mistake 1 — Running reset again before checking reflog

If something goes wrong, investigate first.

❌ Mistake 2 — Using the wrong reflog entry

Always inspect the commit:

git show <commit-hash>

❌ Mistake 3 — Immediately using --hard

A safer recovery approach is often to create a temporary branch first:

git switch -c recovery-work <commit-hash>

❌ Mistake 4 — Treating reflog as a remote backup

Reflog is primarily local. It should complement, not replace, proper remote backups and repository workflows.

❌ Mistake 5 — Waiting too long

Unreachable Git objects may eventually be cleaned up, so recovery should be investigated promptly.


28. Complete Git Reflog Recovery Workflow

When you accidentally lose work, follow this sequence:

# 1. Stop and inspect the repository
git status

# 2. View reference history
git reflog

# 3. Find the suspected commit
git show <commit-hash>

# 4. Create a safe recovery branch
git switch -c recovery/my-work <commit-hash>

# 5. Verify the files
git status

# 6. Review the history
git log --oneline

# 7. Run Laravel tests
php artisan test

Only after confirming the recovery should you decide how to integrate the recovered work back into your normal branch.


29. Git Reflog Cheat Sheet

Command Purpose
git reflog View HEAD reflog
git reflog --all Inspect reflogs across references where available
git reflog show main View reflog for a branch
git show HEAD@{1} Inspect a previous HEAD position
git show <hash> Inspect a specific commit
git switch -c recovery <hash> Create a recovery branch
git reset --hard <hash> Move current branch to a specific commit
git log --oneline Inspect reachable commit history

30. Git Reflog Interview Questions

Q1. What is Git Reflog?

Git Reflog records local movements of references such as HEAD and branches. It is commonly used to find previous positions and recover lost commits.

Q2. What is the difference between git log and git reflog?

git log shows reachable commit history, while git reflog records reference movements in the local repository.

Q3. Can reflog recover a deleted commit?

It can often help recover a commit that is no longer reachable from the current branch, provided the relevant reflog entry and Git object are still available.

Q4. Can Git Reflog recover a deleted branch?

Often yes. You can find the commit where the branch was last pointing and recreate the branch from that commit.

Q5. How do you recover after git reset --hard?

git reflog
git show <commit-hash>
git switch -c recovery <commit-hash>

Q6. Is reflog available on the remote repository?

Reflog is primarily a local mechanism. You should not assume your remote repository provides you with the same local reflog information.

Q7. Is Git Reflog a backup?

No. Reflog is a local recovery mechanism, not a substitute for a remote repository or proper backups.

Q8. What does HEAD@{1} mean?

It refers to the previous reflog entry relative to the current HEAD position.

Q9. Can reflog recover an accidentally rebased branch?

It can often help locate the branch position before the rebase, allowing you to create a recovery branch and inspect the previous state.

Q10. What should you do before resetting to a reflog entry?

Inspect the commit with git show and preferably create a temporary recovery branch first.


31. Easy Way to Remember Git Reflog

Remember this simple sentence:

Git Log tells you where the project history went. Git Reflog tells you where your references went.

For recovery, remember:

REFLOG
  ↓
FIND
  ↓
SHOW
  ↓
RECOVER
  ↓
VERIFY

In commands:

git reflog
git show <hash>
git switch -c recovery <hash>
php artisan test

32. Final Example

Imagine you are working on a Laravel project and accidentally execute:

git reset --hard HEAD~2

You think two days of work are gone.

Instead of panicking, run:

git reflog

Find the previous position:

a82d921 HEAD@{1}: commit: Add pharmacy payment tests

Inspect it:

git show a82d921

Create a recovery branch:

git switch -c recovery/pharmacy-payment a82d921

Verify the Laravel project:

php artisan test

Now the supposedly "lost" work is available again.


Conclusion

Git Reflog is one of the most valuable Git troubleshooting and recovery tools.

It is especially useful when working with commands that change history, such as:

  • git reset
  • git rebase
  • git commit --amend
  • Branch deletion
  • Other HEAD/reference movements

The most important recovery pattern to remember is:

git reflog
      ↓
Find the old commit
      ↓
git show <commit>
      ↓
Create recovery branch
      ↓
Verify your code
      ↓
Restore the work safely

When something appears to disappear from Git, check the reflog before assuming the work is permanently lost.


What's Next?

Now that we understand Git recovery, the next important topic is working with remote repositories.

Next Article: Git Remote, Fetch, Pull and Push Explained

We will cover:

  • git remote
  • What is origin?
  • git fetch
  • git pull
  • git push
  • Remote-tracking branches
  • Upstream branches
  • Fetch vs Pull
  • Push vs Pull
  • Practical Bitbucket workflow
  • Common Git interview questions

Was this useful?

No ratings yet — yours can be the first.

Discussion

Notes from readers. Comments appear after a short review.

No comments yet. Start the thread below.