Deleted commits in GitHub are almost always recoverable if they exist somewhere in the local clone's object database or in the remote's reflog and you act before garbage collection runs. The workable path is: stop making destructive changes, find the commit hash through git reflog or the GitHub API, then restore it onto a branch with a new commit, a revert, or a reset. The key condition is that the commit object still exists on at least one clone or on GitHub's servers. If it has already been pruned from every copy, no technique can bring it back.

This guide covers the ordered steps for each common situation, distinguishes a "deleted commit" from a "deleted branch" and a "force-pushed" remote, and explains where the recovery limits are. Git is designed so that almost nothing is truly gone the moment you delete it; deletion removes references, not the underlying objects, until housekeeping cleans them up.

What "Deleted" Actually Means in Git

A commit is an immutable object identified by a SHA-1 (or SHA-256 in newer repositories) hash. Branches, tags, and HEAD are just movable pointers that reference commits. When you delete a branch, run git reset --hard, or someone force-pushes over history, you're removing or moving references. The commit object often survives in three places:

Your local reflog, which records where HEAD and branch tips pointed for roughly 90 days by default. The remote's reflog on GitHub, which keeps recent ref updates accessible through the web UI and API. Dangling objects in the local object store, which persist until git gc (garbage collection) prunes them.

Understanding this distinction matters because the recovery method depends on which reference was lost and which copy still holds the object. The clock is the real constraint: GitHub's own reflog retention and your local garbage-collection schedule both quietly expire the evidence.

Prerequisites Before You Touch Anything

Before running any command, set yourself up to succeed:

1: Stop writing to the affected repository. Especially avoid git gc, git prune, git reset --hard, and force-pushes. Any of these can remove the object you're trying to recover. 2: Create a safety backup. Copy the entire .git directory or clone the repository to a separate folder. On most systems, cp -r project project-backup (or the equivalent) preserves everything, including the reflog. 3: Confirm you have access to the repository with push rights if you'll be restoring a remote branch, and know the approximate commit message, author, or date to narrow the search. 4: Note the current branch and status with git status and git log --oneline -5 so you can return to a known state.

If the repository was cloned recently and never fetched the old commits, your local copy may not have the objects at all. In that case you'll need GitHub-side recovery, covered below.

Step 1: Find the Lost Commit Locally with the Reflog

The reflog is the first place to look for anything that was recently removed from your own machine. It records every movement of HEAD and branch tips.

Run:

You'll see lines like abc1234 HEAD@{3}: reset: moving to HEAD~1 or def5678 HEAD@{5}: commit: Fix login bug. Scan for the commit you lost. To see the reflog for a specific branch:

Each entry includes a hash and a timestamp. Once you spot the candidate, inspect it before restoring:

This displays the commit message, author, date, and diff. Compare against what you remember losing. If you're unsure, git log -g --oneline gives a condensed reflog view. The success check here is simple: the commit you want appears with its full message and changed files.

Failure case: If the reflog is empty or doesn't contain the commit, either the reflog expired (default 90 days for reachable entries, 30 days for unreachable ones), or the commit was never local to this machine. Move to the next step.

Step 2: Recover Dangling Commits with fsck

If the reflog no longer lists the commit but the object hasn't been garbage-collected, git fsck can find orphaned commits still sitting in the object store.

Run:

Or for a fuller listing:

Unreachable commits appear as lines beginning with unreachable commit . For each candidate, run git show to identify the one you want. git fsck --lost-found also writes unreachable objects into .git/lost-found/commit/ and .git/lost-found/other/ as files named by hash, which is handy when you need to preserve them.

This method works because commits only vanish when garbage collection prunes them, and gc skips objects that are still referenced anywhere, including reflogs. Once you identify the hash, you have what you need to restore it in Step 4.

Limit: If git gc --prune=now has already run, or the default automatic gc fired after two weeks of dormancy, the objects may be gone from your local clone. Check the GitHub side next.

Step 3: Recover from GitHub's Remote Reflog and API

GitHub keeps a server-side reflog for each repository and exposes it through the web interface and the REST API. This is the most reliable path when the commit was pushed at least once and then removed by a force-push, branch deletion, or history rewrite on the remote.

Via the web UI: Navigate to the repository on GitHub, open the branch or commit view, and use the "Activity" tab. You can often reach the old commit through the URL pattern if you know or can guess the short hash—GitHub resolves it if the object still exists. For deleted branches, the branches page sometimes retains an "events" record; checking the repository's "Network" graph can reveal the prior ref.

Via the API: The commit and ref endpoints help. To list recent events on a repository, query the events API (for example, GET /repos/OWNER/REPO/events), and to look up a specific commit that may be unreferenced, request GET /repos/OWNER/REPO/commits/SHA. GitHub returns the commit if the object is still reachable on the server. You can also check pull requests, since a commit that went through a PR is often preserved in the PR's commit list even after the branch was deleted.

A practical tactic: search the repository's pull requests and issues for the commit message or the author's name. If the lost work was ever merged or proposed, GitHub typically retains it in the PR timeline. Fetching that PR's head ref (git fetch origin pull/NUMBER/head:recovered) can pull the commit down.

Important caveat: GitHub does not guarantee indefinite retention of unreferenced objects. Force-pushed commits and deleted branches may become unreachable eventually, and GitHub's support team won't retrieve them on request. Treat this path as time-sensitive—recover as soon as you notice the loss.

Step 4: Restore the Commit Onto a Branch

Once you have the hash (from git reflog, git fsck, or the GitHub API), you have several restoration options depending on what you want the final history to look like.

Option A — Recreate the branch at the recovered commit. If you lost a whole branch:

The branch now points at the old commit. You can merge it, cherry-pick from it, or push it back to the remote with git push -u origin recovered-branch.

Option B — Bring specific commits back onto the current branch. If you want only some work:

This creates a new commit with the same changes on top of your current branch. Use a range (git cherry-pick A..B) to replay a series.

Option C — Move a branch tip backward. If you reset a branch and want it back where it was:

Be careful: this discards uncommitted work on the current branch. Run git stash first or use git reset --soft if you want to keep changes staged in the working tree.

Option D — Restore a remote branch after a force-push. Fetch the recovered commit locally, create or update your branch to point at it, then push with force:

--force-with-lease is safer than plain --force because it aborts if someone else pushed in the meantime.

Success check: After any restore, run git log --oneline --graph --all to confirm the commit is now reachable from a branch. Then git status should show a clean or expected state, and git show should display the recovered changes intact.

Step 5: Handle Force-Pushed History Without Losing Other People's Work

Force-pushing a restored commit over a shared branch can erase teammates' commits. Before rewriting a shared remote:

1: Tell collaborators and confirm nobody is mid-push. 2: Fetch the current remote state so you know exactly what you'd be overwriting: git fetch origin. 3: Compare the remote's current tip with your recovered commit: git log --oneline origin/main..abc1234 and vice versa. 4: Prefer a revert over a force-push if the intent is simply to undo a bad commit that's already published. git revert abc1234 creates a new commit that cancels the changes, preserving history for everyone. Force-push only when you must restore the exact historical shape (for example, after an accidental history rewrite). 5: Coordinate via a branch: push your recovery to a new branch first, open a pull request, and let it merge through normal review. That preserves the recovered work without destabilizing main.

If the repository has branch protection enabled, force-pushing may be blocked entirely. In that case, recovery flows through a revert commit or an administrator temporarily lifting protection, which is a governance decision, not a technical one.

Step 6: Recover Deleted Files and Commits from a Specific Point in Time

Sometimes the request is subtler: a file was deleted in an old commit, or you need the repository exactly as it was on a given date. Two tools handle this.

Restore a single deleted file:

This lists commits that deleted the file. Take the commit just before the deletion:

That restores the file into your working tree, where you can commit it as a new change.

Inspect the repository at a past date: Combine git reflog timestamps with git show : to read any file as it existed at that commit. For browsing the whole tree at a point in time, use git checkout in a worktree:

The separate directory holds the old tree without disturbing your current checkout.

Where Recovery Fails and What to Do Then

Recovery has hard limits, and it's better to recognize them early than to chase impossible outcomes.

Garbage collection has already pruned the object. If git fsck reports nothing unreachable, git reflog is empty of the commit, and the GitHub API returns 404 for the hash, the object is most likely gone from all your copies. Git deliberately deletes unreferenced objects eventually (gc.reflogExpireUnreachable defaults to 30 days). There is no supported technique to reconstruct a commit from its hash alone.

The commit was never pushed. If the work existed only in a local clone that has since been deleted or reinstalled, and the reflog expired, the data is unrecoverable. Git offers no server-side backup for local-only history.

Reinstallation or disk failure wiped .git. A fresh clone won't contain old local commits. The only remaining hope is GitHub-side retention (Step 3), which depends on whether the commit was ever pushed and how long ago it became unreachable.

Enterprise and self-hosted GitHub. GitHub Enterprise Server keeps its own reflog and backup policies, and repository administrators (not ordinary users) may have access to server-side backups. For anything involving audit-relevant history, involve your organization's Git administrators rather than attempting recovery yourself.

When you've exhausted local and remote options and the work still matters, the realistic stop condition is to contact GitHub Support or your organization's platform administrators with the repository name, the approximate date, and any known hashes or commit messages. Support won't guarantee recovery of garbage-collected data, and it's worth knowing they can't conjure objects that no longer exist on their storage.

Preventing the Problem Next Time

A few habits make the "deleted commit" emergency rare. Enable git config --global gc.reflogExpireUnreachable 180.days to keep unreachable commits recoverable longer (at the cost of some disk space). Before any risky rewrite—filter-branch, interactive rebase over shared history, hard resets—create a backup branch (git branch backup-YYYYMMDD) and push it. Protect main with branch rules that require pull requests, which also reduces accidental force-pushes. And when you're about to delete a branch, ask whether its commits are merged; git branch -d refuses to delete unmerged work for exactly this reason, while -D overrides it—so prefer the safe form.

The underlying lesson is steady: Git rarely deletes your data at the moment you ask it to. It deletes references, and the objects linger until housekeeping catches up. Recovering a deleted commit is therefore a race against garbage collection, won by acting quickly, checking the reflog before anything else, and preserving a backup before you touch the repository again.