Restoring a previous commit on GitHub means putting your project's files back to the state they had at an earlier point—and how you do it depends on whether you want to keep the intervening work or discard it. Git offers several distinct tools for this: git revert (which undoes a commit's changes while preserving history), git checkout (which copies old file contents into your working directory), and git reset (which moves your branch tip backward, with or without erasing the commits from history). "Restore" is not a single command, and choosing among these tools is the core skill. GitHub itself only hosts the repository; the actual restoration happens in Git on your machine, then gets pushed to GitHub.
Because the word "restore" is ambiguous, this article answers the most common interpretations: restoring a file or folder to an earlier version, restoring the entire repository to the state of a past commit, and undoing a specific commit that introduced a problem. These produce different results and carry different risks, especially on a shared branch.
First, the distinction that determines everything: history-preserving vs. history-rewriting
Almost every question about restoring commits comes down to one choice: should the old commits remain part of the project's recorded history, or should they disappear?
History-preserving operations add new commits that reverse or adjust earlier work. The project's past stays intact and visible. git revert is the main tool here. It is the safe default for anything other people may have pulled. History-rewriting operations move the branch backward so that later commits are no longer on it. git reset and git push --force fall here. These are appropriate for local cleanup or for a branch only you use, but they cause real problems on shared branches because collaborators' copies diverge from the rewritten remote.
A crucial safety net underlies both: Git rarely deletes commits completely. Even after a reset, the old commits still exist in the reflog and can usually be recovered for a window of time (commonly around 30 days before unreachable objects may be garbage-collected, though this depends on configuration). Knowing this turns many scary situations into recoverable ones—but it is not a license to rewrite shared history casually, because the reflog is local, not something your teammates have.
Finding the commit you want to restore
Before any restore, identify the target commit. You can do this from the command line or from GitHub's web interface.
On the command line:
This shows recent commits with shortened hashes and messages. Add --graph --decorate --all for a fuller picture of branches and merges. You need the commit hash (the hexadecimal identifier such as a1b2c3d) or a reference like HEAD~3 (three commits before the current one).
On GitHub's website: open the repository, click Commits, and browse the history. Each commit has a hash you can copy. Opening a commit shows exactly which files and lines it changed, which is invaluable when you are trying to figure out which commit broke something. GitHub also lets you view a file's history ("History" on any file) so you can find the version you want.
If you are hunting for the commit that introduced a bug, git bisect automates the search by binary-searching history, and git log -S"some text" finds commits that added or removed a particular string. These are optional but genuinely useful when the history is long.
Restoring a single file or folder to an earlier version
This is perhaps the most literal reading of "restore a previous commit": you want one file back the way it was, not the whole project.
Use git checkout (or the newer, less overloaded git restore) to copy the old file contents into your working directory:
or, with the modern syntax:
This stages the old version of the file. Review it, then commit:
The key point: this does not delete or alter any commits. It creates a new commit whose content for that file matches the old version. The history stays linear and safe to push. You can restore an entire directory the same way by naming the directory instead of a file.
One common confusion: git checkout a1b2c3d without a path puts you in "detached HEAD" state—you are viewing the old commit, not on a branch. It is fine for inspection, but any commits you make there are not attached to a branch. If you actually want the whole project to look like that old commit, see the next section. To leave detached HEAD, run git switch - or git checkout main.
Restoring the whole repository to a previous commit
If you want the entire working tree to match an earlier commit, the question is again whether later commits should survive.
The safe, history-preserving approach: revert
git revert creates a new commit that undoes the changes of one or more specified commits. To undo a single commit:
To undo a range (for example, everything after a known-good commit good123):
Reverting a range applies the inverse of each commit in sequence, then lets you commit the result as one commit. This is the standard way to "roll back" a shared branch without breaking anyone else's clone. The history grows; nothing is erased. Note that reverting a merge commit requires specifying which parent to treat as the mainline (-m 1 or -m 2), which trips people up; if the range includes merges, read the error message carefully.
git revert is not magic: if later commits built on the changes you are reverting, the revert may conflict, and you resolve conflicts just as with a merge. The result is a codebase that no longer contains the reverted changes, but with a clear record of what happened and why.
The history-rewriting approach: reset
If the branch is local-only, or you are certain no one else depends on it, git reset moves the branch tip. There are three modes, and the difference matters:
| Command | Branch tip | Staging area | Working directory | Typical use |
|---|---|---|---|---|
| git reset --soft | Moves back | Unchanged (changes staged) | Unchanged | Squash or re-commit later work |
| git reset --mixed (default) | Moves back | Reset to match commit | Unchanged | Unstage changes, re-review |
| git reset --hard | Moves back | Reset | Reset (changes discarded) | Truly discard later work |
For example, to make the branch look exactly like commit a1b2c3d and throw away everything after it:
Be careful with --hard: uncommitted changes are destroyed, and they are not recoverable through the reflog the way committed work is. If you have unsaved work, stash it first (git stash) or commit it.
To send a rewritten branch to GitHub, you must force-push:
Prefer --force-with-lease over plain --force: it refuses to overwrite the remote if someone else pushed in the meantime, protecting you from clobbering a teammate's work. Even so, force-pushing a shared branch is disruptive—collaborators must reset their local branches—so reserve it for branches you alone own, or coordinate first. Many teams protect important branches on GitHub so that force-pushes are blocked entirely; if your push is rejected for that reason, that is a signal to use revert instead.
An alternative that avoids touching the existing branch is to create a new branch at the old commit and open a pull request from it, or to revert on a fresh branch and merge. This keeps main's history untouched while still restoring the desired state—often the cleanest answer for a team project.
Recovering from a mistake: the reflog
If you reset too far, force-pushed the wrong commit, or otherwise "lost" commits, the reflog is your first stop:
This lists where HEAD and branches have pointed recently, including commits no longer on any branch. Find the entry before the mistake and reset or branch from it:
or, to preserve the current state while recovering old work:
On GitHub's side, the repository's Activity view and the commit pages still show commits that were force-pushed away for a while, and pull requests retain their commit references. But the reflog is local and expires; it is a recovery tool, not a backup strategy.
GitHub-specific details worth knowing
You cannot run these commands on GitHub itself. The web interface lets you browse history, view diffs, revert a single commit through a pull request (the "Revert" button on a merged pull request creates a revert PR), and edit files, but real restoration is done with Git locally and pushed. Cloning the repository first is the normal starting point.
Reverting a merged pull request through GitHub's "Revert" button is convenient and history-preserving: it opens a new pull request that undoes the merge. This is the web-only path closest to git revert and is a good fit when the problematic change arrived as a PR.
Protected branches may reject force-pushes and may require pull requests. If git push --force-with-lease fails with a protection error, do not fight it; switch to revert or a new branch.
Deleted branches can often be restored from the GitHub interface if they were deleted recently, and their commits remain reachable through associated pull requests. This is a different scenario from restoring a commit, but it comes up in the same searches.
Choosing the right method: a practical guide
One file or folder, keep history intact: git checkout -- , review, commit. Safe to push anywhere. Undo a specific bad commit on a shared branch: git revert . Safe to push; history grows. Roll a shared branch back to an earlier state: git revert ..HEAD (or revert the relevant merges individually), then push normally. Never force-push a shared branch. Local branch, personal cleanup, nobody else involved: git reset --hard (after stashing or committing anything you want to keep), then git push --force-with-lease. You want the old state without altering the current branch's history: create a branch at the old commit and merge or PR it, or revert on a new branch. You already made a mess: check git reflog before doing anything else.
A useful mental model: revert is "undo the change but keep the record," reset is "pretend the later commits never happened," and checkout/restore is "bring back these files." The first is collaborative; the second is surgical and private; the third is targeted.
Common mistakes and how to avoid them
Force-pushing a shared branch. This is the mistake with the widest blast radius. It rewrites history others have based work on. If you must rewrite, coordinate, use --force-with-lease, and prefer protected branches that prevent accidents.
Using reset --hard with uncommitted work. Uncommitted changes are not in the reflog and are genuinely lost. Stash or commit first.
Confusing detached HEAD with a restored branch. Checking out an old commit to look around does not restore anything; commit or branch if you want to keep the result.
Reverting a merge without specifying the mainline. Git will refuse and explain; read the message and pick the correct parent. Reverting merges is the most error-prone part of revert, so test on a branch first.
Expecting GitHub's web UI to run Git commands. It does not. Use it to inspect history and to revert merged PRs; use local Git for everything else.
Forgetting to push. A local revert or reset changes nothing on GitHub until you push, and a reset requires a force-push—another reason to think before choosing reset.
The short version
To restore a previous commit on GitHub, identify the commit with git log or the GitHub Commits page, then choose based on whether history should be preserved. To keep history—the right default for shared branches—use git revert for whole commits or git checkout -- for individual files, and push normally. To rewrite history on a branch only you use, use git reset --hard followed by git push --force-with-lease, accepting that collaborators must resynchronize. If something goes wrong, git reflog is your recovery path for committed work. The single most important habit is to prefer history-preserving operations whenever anyone else might have pulled the branch.