---
title: "Lost a commit after a reset? git reflog brings it back"
description: "git reflog records every time HEAD or a branch tip changed in your local repository. So you can find a commit lost after a reset or rebase there, and put a new branch on it with one command."
url: https://ihsanalapsi.dev/articles/git-reflog-recover-commits
lang: en
date: 2026-11-24
updated: 2026-11-24
tags: ["Developer tools", "Web development"]
author: Ihsan Alapsi (إحسان العبسي)
---
# Lost a commit after a reset? git reflog brings it back

You ran `git reset --hard` on the wrong branch, or a rebase went wrong, and the commit you worked on no longer shows in `git log`. The work is usually not gone: Git keeps its own record of where each branch was. By the end of this article you will bring the commit back on a new branch, without touching the branch you are on.

## What is git reflog?

`git reflog` is a local log that records every time the tip of a branch or another reference, like `HEAD`, changed in your repository. So even when `git log` no longer shows the commit, the reflog remembers that `HEAD` was on it.

[The official git reflog documentation](https://git-scm.com/docs/git-reflog) says these logs record when the tips of branches and other references were updated in the local repository. The `show` subcommand is the default: `git reflog` on its own shows the log of `HEAD`.

Think of it like the visitor book at a building's front door. The flats may change, but the book still says who came in and where they went.

## How do you read its lines?

Each line in the reflog is a place `HEAD` used to be, and it has a name like `HEAD@{2}`, meaning "where HEAD used to be two moves ago".

The same documentation gives another example: `master@{one.week.ago}` means where `master` pointed one week ago in this local repository. So you can ask by number of moves, or by time.

```bash
git reflog
git reflog show master
```

The first command shows the log of `HEAD`, the second only the log of the `master` branch.

## How do you bring the lost commit back?

You bring it back by finding its line in the reflog and putting a new branch on it, so the commit is again somewhere you can reach by name.

```bash
git reflog
git branch rescue HEAD@{2}
git switch rescue
```

Per [the git branch documentation](https://git-scm.com/docs/git-branch), the command creates a new branch head that points to the start-point you give it, or to `HEAD` if you give none. It does not switch you to it, which is why `git switch` comes next. Replace `HEAD@{2}` with the line that has the message of the commit you want.

This is safer than moving your branch back: if you picked the wrong line, the original branch is still as it was, so you delete `rescue` and try another line.

## How long does the commit stay in the log?

With the default settings, a line the branch tip no longer reaches stays 30 days, and the other lines stay 90 days.

The documentation says `git reflog expire` takes the time from `gc.reflogExpireUnreachable`, which defaults to 30 days, for lines not reachable from the current branch tip, and from `gc.reflogExpire`, which defaults to 90 days, for the rest. So if the lost commit is no longer reachable from any branch, it is the first kind, and you should not put off getting it back.

## Where do you start now?

Open any repository you are working on now, type `git reflog`, and read the first ten lines: you will see your last moves, in order. That way, when something goes missing, you know where to look.

If the wrong commit was already pushed to the remote, the path is different: [git revert for a pushed commit](/articles/git-revert-pushed-commit). When you do not know which commit broke things, [git bisect finds it](/articles/git-bisect-find-bad-commit). And if you want to work on two branches at once without a stash, there is [git worktree](/articles/git-worktree-parallel-branches). The last time you lost a commit, how did you get it back?

## FAQ

**Does the reflog reach the remote?**

No. The documentation says it records the updates in the local repository, so it lives on your machine.

**Why a new branch and not move the same branch back?**

Because git branch makes the new branch without touching the branch you are on, so you lose nothing if you picked the wrong line.

**Can I see where a branch was a week ago?**

Yes. A name like master@{one.week.ago} means where master pointed one week ago in this local repository.
