You can usually recover a deleted GitHub file, but the method depends on how the deletion happened. If it was committed and pushed to a repository you own, the file almost always lives in Git history—even when the working tree no longer shows it. Undo a commit you just made with git restore or a revert, or pull the last version from the commit before deletion. If nothing was committed yet, restore from your local editor or filesystem trash. Only when history was rewritten or the repository itself was deleted do you need GitHub Support or an external backup.

What follows is a practical order of operations, newest and cheapest first, with the commands and success checks for each. The steps assume a normal Git repository on GitHub and a file that existed at some point in a commit; they don't cover recovered content lost from the GitHub web editor before any save, which leaves no trace in the repository itself.

Before You Touch Anything: Prerequisites and a Safety Check

Gather three things first. You need the repository name and the branch that contained the file, because a file can exist on one branch and be absent on another. You need command-line Git installed locally, or access to the GitHub web interface if you prefer not to use a terminal. And you need a rough sense of when the file existed—a date range, a commit message, or the pull request that introduced it—so you can narrow the search.

Do a reversible safety check before any recovery attempt: confirm whether the file truly is gone from the current branch or simply missing from your local checkout. Run git status and git log --oneline -5 in the repository. If your local branch is behind the remote, a git pull might bring the file back because someone else restored it or because you're looking at stale state. If git log shows the deletion commit at the top, the file was removed deliberately and history is your path. If your working directory was wiped without a commit, skip to the local recovery section—Git never saw the deletion, so there's nothing in history to find.

One more precaution: work on a scratch branch or in a separate clone for anything beyond a simple restore. Recovery operations like reverts and cherry-picks create new commits, and doing them on a throwaway branch keeps your main line clean until you're sure the content is correct. Nothing in the basic restore steps below is destructive, but history rewriting later in this guide can be, and a scratch branch makes that difference explicit.

Method 1: Undo an Uncommitted Deletion With git restore

This is the fastest case and the most common one: you deleted a file in your working directory and haven't committed the deletion. Git still has the file in the index or in HEAD, so a single command brings it back.

1: Check what Git thinks happened: git status. A deleted-but-unstaged file appears under "Changes not staged for commit" as deleted: path/to/file. 2: Restore from the index (the last staged version) with git restore path/to/file. 3: If the file was already staged for deletion, restore from the last commit instead: git restore --source=HEAD -- path/to/file. 4: Confirm with git status that the file no longer appears as deleted, then open it to verify the content.

The success check is simple: the file reappears with the content it had at the last commit or staging point. The failure mode to know about is restoring over a file you've since recreated with different content—git restore overwrites the working copy, so if you're unsure, copy your current version somewhere else first. This method only recovers what Git already tracked; it does nothing for a file that was never added or committed.

Method 2: Find the Last Good Commit and Pull the File Out

Once a deletion has been committed and pushed, the file is gone from the current branch but still present in the commit just before the deletion. You want to locate that commit and extract the file's content from it. There are two ways to do this, and they suit different comfort levels.

The web interface route

On GitHub, open the repository and click the commit history for the file's former path—or, if the file link is dead, browse the repository's commit list and look for the commit titled something like "Delete filename" or a merge that removed it. Open the commit immediately before the deletion, click "Browse files" at that commit, navigate to the file, and use the "Raw" button to view or download its exact content. Then recreate the file at the current branch and commit it. This route needs no local Git and is the right choice for a one-off recovery or for someone who isn't comfortable with the command line.

The terminal route

Identify the deletion commit, then the commit before it, then restore the file from that earlier commit.

1: Find the deletion commit: git log --oneline -- path/to/file. The newest entry touching that path is typically the deletion; note its hash. 2: Find the parent commit of the deletion: git log --oneline -1 ^ (the caret means "parent of"). 3: Restore the file from the parent into your working tree: git checkout -- path/to/file. 4: Confirm the file exists and looks right, then commit it: git add path/to/file and git commit -m "Restore path/to/file".

If you don't know the path anymore, search by content instead of filename: git log --all --oneline -S "a distinctive string from the file" finds commits that added or removed that string, which often locates the file even after a rename. Expect a list of commit hashes; the one paired with the deletion is your starting point.

The success check is that the restored file matches the version you expected. A useful verification is git diff -- path/to/file, which should show no differences for that file if the restore was faithful. The limitation: this method assumes the deletion commit is reachable from some branch or tag. If the branch was force-pushed or deleted, jump to the history-rewrite section.

Method 3: Revert the Deletion Commit Cleanly

When you want the restoration recorded as its own commit—useful on shared branches where rewriting history would disrupt collaborators—revert the deletion rather than re-extracting the file.

1: Identify the deletion commit hash as in Method 2. 2: Run git revert . 3: Resolve any conflicts if the file's neighbors changed since deletion; Git will tell you which paths conflict. 4: Push the revert commit to the remote.

The result is a new commit that reintroduces the file's content as it stood before deletion. The success check is that git log shows the revert on top and the file is present on the branch. This is the safest option for teams because it doesn't rewrite anything anyone else has based work on. The trade-off is that the file's history now includes a delete-then-restore pair, which is slightly noisier than a clean extraction but far less disruptive than rewriting.

Method 4: Recover From a Force-Push or Rewritten History

Sometimes the file isn't just deleted from the tip—the branch was force-pushed, rebased, or reset so that the commit containing the file is no longer reachable from any branch. The content still exists as an unreachable object in the repository until Git's garbage collection removes it, and on GitHub there's a window to catch it.

Start locally with the reflog, which records where branch tips have pointed: git reflog shows recent HEAD movements, and git reflog show narrows to one branch. Find the entry before the destructive operation, then restore the file from that commit with the checkout command from Method 2. If the reflog has already expired locally, check whether another clone or a teammate's machine still has the old commits—any clone that fetched before the rewrite retains them, and git log --all there can find the file.

On GitHub itself, rewritten history isn't instantly destroyed. GitHub keeps unreachable commits accessible through the repository's network and activity views for a limited period, and pull requests that referenced the old commits preserve them in their own refs. The practical move is to open the pull request that originally added or last modified the file: GitHub keeps a PR's commits even after the source branch is deleted or rewritten, so the file's diff and raw content are often still viewable from the PR page. For a branch reset on the server, GitHub Support can sometimes restore a repository's prior state within a short window after the event, but that's a last resort that depends on timing and isn't guaranteed—treat it as a request, not a recovery method.

A version-specific caution: force-push behavior on protected branches is restricted by branch protection rules and repository settings, and those settings vary by organization and plan. If a protected branch rejected the force-push, the history may be intact and Method 2 already works; if it succeeded, the reflog and PR route above is your best bet.

Method 5: When Nothing Was Committed—Local Editor and Trash Recovery

Git can't help with a file it never tracked, so this path is purely about the tools that held the content before Git did. If you deleted an untracked file in your editor, the editor's local history is the first place to look. Many editors keep per-file undo history or a local history feature that survives closing and reopening; check the editor's documentation for where that store lives and how far back it goes, since retention windows differ by version and settings. If the editor has none, check the operating system's trash or recycle bin, which usually retains deleted files until emptied and may hold the file if the deletion went through the normal file manager.

If the file was created in the GitHub web editor and never committed, there's no repository copy to recover—the web editor's content lives in the browser session and isn't part of Git history until saved. In that specific case, browser history, a cached page, or the editor's draft state is the only hope, and those aren't recoverable through GitHub.

The success check for this section is simply that the file's content appears and matches what you remember. The limit is retention: trash and editor history are sized and aged by your own settings, so recovery odds drop sharply with time, and nothing here is guaranteed once the underlying store has rotated.

Find the File When You Don't Remember the Path

A deleted file's path is often the hardest part of the search, especially after renames. Three techniques cover most cases.

Search by content with git log --all -S "" --oneline, which lists commits that changed the number of occurrences of that string; the commit where the string disappears is a strong candidate for the deletion. Search by filename fragment with git log --all --oneline -- "partial-name", since Git's pathspec supports wildcards. And when the file was renamed before deletion, add --follow to a log on the surviving path to trace its earlier names: git log --follow --oneline -- path/to/current-name. Each of these works on the full set of refs with --all, which matters because the committing branch may no longer exist while the commits remain reachable through other refs.

Expect some noise: -S matches any commit that changes the string's count, including refactors, so scan the output for commits whose message or date lines up with the loss. The check is that the candidate commit's diff shows the file being removed; if it does, you've found the deletion and can proceed with Method 2 or 3.

Recovering a Deleted Repository, Not Just a File

When the whole repository was deleted, individual file recovery is moot—you need a copy of the repository from somewhere else. The strongest source is a clone on any machine or CI system that fetched it before deletion, including automated builds and teammates' laptops; a full clone contains all branches and tags that were fetched, so git log --all there finds the file. GitHub itself also retains deleted repositories for a limited period and can restore them on request through Support within that window, but the retention period and eligibility depend on account type and the circumstances, so contact Support promptly rather than assuming a fixed deadline. Forks are another copy: if anyone forked the repository, the fork still holds the history up to the fork point and possibly more if it was kept in sync.

What you can't do is restore from a shallow clone or a source archive alone—neither contains the full commit graph needed to walk back to the file. If your only copy is a zip download of the code, you can recover the file if it's present in that snapshot, but you can't recover versions that were deleted before the snapshot was taken.

After Recovery: Preventing the Same Loss

The recovery steps above work, but the ones that succeed reliably are the ones that happen before a deletion becomes expensive. A few habits make the loss rarer without imposing much overhead.

Commit early and often, even to a scratch branch, so that untracked-but-important work becomes tracked. Use branches for experiments so a reset or force-push on a feature branch can't touch your main line. Protect important branches with rules that block force-pushes and deletions; that keeps a rewrite from erasing history in the first place, and it's a repository setting rather than a workflow burden. Keep at least one full clone outside your primary machine, whether on a teammate's system, a CI runner, or a mirror, so the repository has a second home. Finally, for files you'd hate to lose, remember that GitHub's own web editor history is not a backup—commit rather than leaving content sitting in a draft.

How the Methods Compare

The right method depends mostly on where the deletion happened and how much history rewriting is involved. The table below maps each scenario to its recovery path, the typical evidence you need, and the main limit. It restates the choices already explained above as a quick reference, not as new procedures.

Scenario Best method What you need Main limitation
Deleted locally, not committed git restore from index or HEAD Local repo, file still in index/HEAD Only works if Git already tracked it
Committed and pushed deletion Extract from parent commit or revert Deletion commit hash Fails if commits are unreachable
Force-push or rewritten branch Reflog, teammate clone, or PR refs Old commit reachable somewhere GitHub retention window is limited
Never committed (editor/file manager) Editor local history or OS trash Untouched retention store Ages out with your own settings
Repository deleted Clone, fork, or GitHub Support Existing copy elsewhere Support restore window varies

What to Do When the File Isn't Where You Expected

A few failure patterns account for most "I can't find it" cases, and each points to a different next step.

If git log -- path returns nothing, the path was probably different when the file existed—try --follow or a content search. If the commit exists but git checkout says the path is ambiguous or unmatched, you may be restoring from a commit where the file was already renamed; check git show --stat for the actual path at that commit. If the reflog is empty, the repository may have been freshly cloned after the rewrite, in which case only a remote copy or GitHub's server-side retention can help. And if a restore produces a file that's empty or truncated, you may be reading a binary or LFS-tracked file through a text-oriented path—check whether the repository uses Git LFS, since large-file content lives in a separate store and the pointer file alone won't reconstruct it. For any of these, stop after the reversible checks (reflog, PR refs, second clone) and hand off to GitHub Support or a teammate with a pre-rewrite clone before attempting history manipulation on the server.

The general rule that ties all of this together: recovery is easiest at the moment of deletion and gets harder with each step—commit, push, rewrite, repository deletion. When you notice a file missing, run the checks in the order this guide presents them, verify each result with the success check, and keep a copy of anything you recover before doing anything else to the repository.