# The Reflog

> How to use `git reflog` as your ultimate safety net to recover lost commits and branches.

Subject: Version Control | Lesson 21 of 29 | 6 min read
Interactive lesson: https://codewithanimation.in/git/git-reflog
Previous: [Undoing Commits: Reset](https://codewithanimation.in/git/git-reset.md) | Next: [Rebasing Basics](https://codewithanimation.in/git/git-rebase.md)

## The git reflog command

`git reflog` (Reference logs) is a local mechanism that tracks updates to the tip of branches and the `HEAD` pointer.

It acts as a chronological, hidden diary of every action that changes where your local `HEAD` points (e.g., commits, checkouts, resets, merges, and rebases).

## Why it matters

It acts as your ultimate safety net.

If you accidentally delete an unmerged branch, mangle a complex rebase, or perform a destructive `git reset --hard` that removes code you actually needed, those commits are not instantly gone. The reflog lets you find the hash of the 'lost' state and restore your work exactly as it was.

## How it works

The reflog maintains a hidden diary of every time `HEAD` moved locally.

When you run a command that moves HEAD, Git logs the old commit hash, the new commit hash, and a brief description of the action.

You can view this timeline using `git reflog`, identify the specific step where things went wrong, and use `git reset --hard <hash>` to travel back to the exact commit hash before the mistake occurred.

## Common mistake: The 'Deleted Forever' Misconception

Many developers think that destructive commands like `git reset --hard` or deleting a branch permanently deletes their code forever.

In reality, your commits are still securely stored in your local Git database. As long as you actually committed the code, you can recover it. The commits are only permanently deleted when Git's background garbage collection cleans up 'unreachable' commits (typically after 30-90 days).

## Tip: The Time Machine

Mental Model: Think of the reflog as your personal Undo history. When `git log` fails you because a commit is no longer part of your branch's visible history, the reflog will remember exactly where you were.

Just type `git reflog`, grab the commit hash (or pointer like `HEAD@{2}`), and jump back in time.

## Try it: Recover a previous branch position

Inspect local reflog entries and choose the previous commit to recover.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/git/git-reflog

## Check yourself

### Question 1

What does `git reflog` primarily track?

- A. All commits made across all branches by all team members.
- B. Every time the HEAD pointer moves in your local repository.
- C. The exact file differences (diffs) of your uncommitted changes.
- D. A cloud backup of your repository.

Correct answer: B

`git reflog` keeps a chronological diary of every action that moved your local `HEAD` pointer, such as checkouts, commits, resets, and merges.

- Why not A: Reflogs are strictly local to your machine.
- Why not C: The reflog only tracks commit pointer movements.
- Why not D: Reflogs are stored locally, never on the remote server.

### Question 2

You just ran `git reset --hard HEAD~1` and accidentally removed a commit you desperately needed. How can you get it back?

- A. Run `git revert --hard HEAD~1` to undo the reset.
- B. Run `git log --all` and look for the deleted commit.
- C. You cannot get it back; a hard reset permanently deletes the code.
- D. Run `git reflog` to find the lost commit's hash, then run `git reset --hard <hash>`.

Correct answer: D

The reflog acts as a safety net. Even if a commit is detached and invisible in standard logs, its hash is recorded in the reflog, allowing you to easily reset back to it.

- Why not A: `git revert` cannot undo a `git reset --hard` operation.
- Why not B: A hard reset makes the removed commit 'unreachable' in standard logs.
- Why not C: Commits aren't instantly deleted. They remain in the database.

### Question 3

Which of the following statements about `git reflog` is true?

- A. Reflog data is strictly local and is never shared when you `git push`.
- B. Reflogs are kept forever and never expire.
- C. `git reflog` is just an alias for `git log --oneline`.
- D. The reflog tracks changes to tracked files even before they are committed.

Correct answer: A

Your reflog is your personal, local history. Because it is not pushed to the remote repository, your team members cannot see your local mistakes or HEAD movements.

- Why not B: Older unreachable reflog entries expire after 30 to 90 days.
- Why not C: `git log` shows current branch history, `reflog` shows all HEAD movements.
- Why not D: The reflog only tracks commits. It cannot recover uncommitted work.

## Remember this

- The reflog tracks every time your local HEAD moves.
- It is your ultimate safety net for recovering 'deleted' commits or branches.
- Reflog data is strictly local and never pushed.
- It only recovers committed code, not uncommitted changes.
