Git can recover a local commit that seems to have vanished as long as the commit object still exists in your repository — which is usually the case for days or weeks after the commit was made. The reliable method is to find the commit's hash in git reflog (the record of where your branch tips and HEAD have pointed) or in git fsck --lost-found (which surfaces unreachable objects), then create a new branch or reset a branch to point at that hash. The critical qualification is that Git only keeps unreachable commits alive until garbage collection runs, so recovery is a race against git gc, not a permanent safety net.

The phrase "local commit recovery" usually means one of a few situations: a commit was made on a branch that was later reset, rebased, or force-moved; a commit was made while HEAD was detached and then you switched branches; or a branch was deleted along with its commits. Each has a slightly different recovery path, and a genuine git commit that was never created at all — because the command failed or you never ran it — cannot be recovered by any of these methods. This article covers the recoverable cases, the commands, and the reasons they work.

Why lost commits are usually still there

Git's object model is the reason recovery is possible. When you commit, Git writes a commit object containing a snapshot of the tree, a message, author and committer information, and parent links. That object is stored under .git/objects and is addressed by its SHA-1 (or SHA-256) hash. Creating the commit also updates a reference — the branch you're on, or HEAD in detached mode — to point at the new commit.

When you reset a branch, amend a commit, rebase, or delete a branch, Git typically moves a reference. It does not immediately delete the commit objects. Those objects become unreachable: no branch, tag, or other reference points to them, so a normal git log won't show them. But they remain on disk. The reflog is the most convenient record of where references used to point, and git fsck can find objects that even the reflog no longer references.

Two clocks matter. First, reflog entries have expiration defaults: 90 days for reachable entries and 30 days for unreachable ones, configurable via gc.reflogExpire and gc.reflogExpireUnreachable. Second, git gc (garbage collection) prunes unreachable objects older than a grace period, by default two weeks (gc.pruneExpire). Automatic gc can be triggered by ordinary commands when the repository accumulates enough loose objects, so "it's only been a few days" is not a guarantee — but in typical interactive use, a commit made recently is recoverable.

Method 1: Find the commit in the reflog

The reflog is the first place to look, because it shows the sequence of positions each reference has held, with timestamps and the operation that caused each move.

You'll see lines like:

Read it from the top: the most recent action is first. In this example, a reset moved HEAD back one commit, so 9f8e7d6 ("Add retry logic to uploader") is the commit that dropped off the branch. Recovery is a matter of giving that hash a name again:

or, if you want to put the current branch back where it was:

Use --hard only when you're sure you don't need the current working tree state; git reset --soft or --mixed moves the branch without touching files, which is often safer when you're uncertain.

For a specific branch's history rather than HEAD's:

This is useful when you reset a branch other than the one you're on, or when HEAD's reflog has been cluttered by checkouts. Reflog entries are also addressable with the @{n} syntax: main@{1} means "where main pointed one move ago," and HEAD@{yesterday} uses date syntax.

Recovering an amended commit

git commit --amend replaces the previous commit with a new one. The old commit is still in the reflog:

The --soft reset moves the branch back to the old commit while leaving the amended content staged, so you can recommit in whatever form you want. If you only need a file or two from the old version, git checkout 7d6c5b4 -- path/to/file extracts them without moving any branch.

Recovering after a rebase

An interactive rebase, especially one that drops commits or squashes them, rewrites history. The pre-rebase tip is recorded in the reflog under an entry labeled rebase (start) or similar, and the original branch position appears as branch@{n}:

Find the entry before the rebase began and branch from it:

Then compare the two branches with git log --oneline --graph feature-pre-rebase feature and cherry-pick or reset as needed. If the rebase is still in progress or you aborted it, git rebase --abort restores the original state directly.

Method 2: Recover detached-HEAD commits

A commit made in detached HEAD state belongs to no branch. When you check out a branch afterward, HEAD moves and the commit becomes unreachable — but it stays in HEAD's reflog:

Git often prints a helpful hint at checkout time ("Warning: you are leaving 1 commit behind...") that includes the exact hash. That message is worth reading rather than dismissing; it's the fastest possible recovery path. If you missed it, the reflog has the same information.

Method 3: Use git fsck when the reflog doesn't help

Reflog entries expire, and some operations don't write the reflog entry you'd expect (or the reflog may have been disabled). git fsck inspects the object database directly and can list unreachable commits:

This prints lines such as unreachable commit 8e9d7c6... and, with --lost-found, writes the objects into .git/lost-found/commit/ and .git/lost-found/other/ as files named by hash. You can inspect each candidate:

git show reveals the message, author, date, and diff, which is usually enough to identify the one you want. Then name it with a branch as above. Because fsck can list many unrelated unreachable objects, sorting by commit date helps:

That pipeline is a convenience, not a requirement; running git show on each hash works just as well for a handful of candidates. Note that git fsck output ordering is not guaranteed and the command can be slow on large repositories.

One caution: git fsck sees objects that gc hasn't pruned, but it does not extend their lifetime. Running git gc --prune=now afterward would delete them. Avoid aggressive pruning until you've finished recovering.

Method 4: Recover a deleted branch

Deleting a branch with git branch -D removes the reference but not the commits. If the branch's tip was recently reachable, git reflog will show a branch: Created from ... entry or the last commit entries for that branch name, and git fsck will find the commits if the reflog has expired.

If the branch was deleted on a remote and you need the local equivalent, git reflog show origin/my-feature or git fsck may still have it locally; alternatively, if the remote still has the commits (for example, reachable from another branch or tag), a git fetch restores the remote-tracking ref. Remote-side recovery is a separate topic and depends on the server's own reflog and gc settings.

What cannot be recovered

Several situations look like "lost commits" but aren't recoverable by these methods:

A commit that was never created. If git commit failed (hooks rejected it, identity wasn't configured, the index was empty), there is no commit object. The staged changes may still be in the index; check git status and git diff --cached. If you ran git add and then lost the index, git fsck --lost-found can sometimes find the staged blobs as unreachable objects, but reconstructing a tree from them is manual work. Unstaged, uncommitted changes. These live only in the working tree. Git does not store them, and no recovery command can retrieve them once overwritten. Editor undo history or OS-level file recovery are the only options. Commits pruned by gc. Once git gc has actually deleted unreachable objects past the prune grace period, the data is gone from the local repository. A backup, a clone that hasn't been gc'd, or a remote copy is the only remaining source. Commits from a different repository. If you cloned fresh, the old clone's objects are not in the new one. Recovery must happen where the commits were made.

There is also a subtler distinction: git reset --hard discards working-tree changes, and if those changes were never committed, they are not in the reflog. The reflog records reference movements, not file contents that never entered the object database.

Defensive habits that make recovery trivial

Recovery works best when there's a trail. A few habits preserve that trail:

Check git reflog before doing anything drastic. The moment you suspect a loss, look — before running more commands that move references. Use git switch -c to adopt a detached commit immediately rather than leaving HEAD detached. Prefer git reset --soft or --mixed when unsure, so the working tree isn't overwritten before you've inspected the target commit. Tag or branch temporary work. A throwaway branch costs nothing and keeps commits reachable, which also exempts them from unreachable-object pruning. Avoid git gc --prune=now during recovery. Default gc's two-week prune grace period is your friend; aggressive pruning removes it. Keep backups or remotes. A commit that exists only locally and has been pruned is unrecoverable; a push or clone is a second copy.

Quick reference

Situation First command Recovery action
Branch reset or moved git reflog git branch or git reset --hard
Commit amended git reflog git reset --soft
Rebase rewrote history git reflog show branch from the pre-rebase entry
Detached HEAD commit abandoned git reflog git branch
Branch deleted git reflog / git fsck --lost-found git branch
Reflog expired or empty git fsck --lost-found inspect candidates with git show, then branch
Commit never created — check index/working tree; unrecoverable if changes were never staged
Objects pruned by gc — restore from backup, clone, or remote

The through-line is that Git almost never destroys commit data at the moment you "lose" it; it just stops pointing at it. The reflog is the fastest map back, git fsck is the fallback when the map is gone, and gc is the deadline. Recover promptly, name the commit with a branch before you run anything that might trigger pruning, and verify with git show or git log before resetting a branch to it.