GitHub

How to Revert to a Previous Commit in GitHub

How to Revert to a Previous Commit in GitHub

You pushed a commit. Something broke. Now what?

Knowing how to revert to a previous commit in GitHub saves hours of manual debugging and prevents production disasters.

Every developer faces this situation. A bug slips through, a feature fails, or code simply needs to roll back to a working state.

Git provides multiple methods to undo changes: git revert, git reset, and GitHub Desktop’s visual interface.

This guide covers each approach with exact commands, step-by-step instructions, and guidance on which method fits your situation.

You will learn how to find commit hashes, preserve or rewrite history, and push reverted changes to your remote branch without breaking your team’s workflow.

How to Revert to a Previous Commit in GitHub

maxresdefault How to Revert to a Previous Commit in GitHub

Reverting to a previous commit in GitHub is the process of undoing changes in your repository by either creating a new commit that cancels out unwanted modifications or resetting your branch pointer to an earlier state.

Developers need this when code breaks after a recent push, when a bug gets introduced, or when a feature needs to be rolled back before production deployment.

This guide covers 5 methods requiring 5-10 minutes and basic familiarity with Git commands.

Prerequisites

Git version 2.30 or later installed on your system.

Why is GitHub the heart of open source?

Uncover GitHub statistics: developer community growth, repository trends, collaboration patterns, and the platform that powers modern software development.

Explore GitHub Data →

A GitHub account with write access to the target repository.

Command line interface: Terminal (macOS/Linux), Git Bash (Windows), or Command Prompt.

Time estimate: 5-10 minutes depending on method chosen.

Skill level: Beginner to intermediate. You should understand staging in Git and basic commit operations.

Step One: How Do You Find the Commit Hash You Want to Revert To?

To find your target commit, run git log --oneline in your terminal to display the commit history with shortened SHA-1 hashes and commit messages; identify the commit you want to restore and copy its 7-character hash identifier.

Looking to sharpen your Git skills? Branching, merging, rebasing, and everything else you need - including git rebase, git stash, and commit workflows - is on one page in the Git Cheat Sheet.

Action

  • Open terminal: Navigate to your local repository folder
  • Run command: git log --oneline
  • Expected output: List showing commit hashes like a1b2c3d followed by commit messages

The commit hash is the unique identifier you will use in subsequent commands.

Purpose

The commit hash serves as the exact reference point for Git operations. Without it, you cannot specify which version of your codebase to restore.

Step Two: How Do You Revert a Commit Using Git Revert?

To revert a commit, run git revert <commit-hash> which creates a new commit that undoes all changes from the specified commit while preserving the complete version history in your repository.

Action

  • Command: git revert a1b2c3d (replace with your target hash)
  • Editor opens: Confirm or modify the default revert commit message
  • Save and exit: Press :wq in Vim or Ctrl+X in Nano
  • Result: New commit appears with prefix “Revert” in the message

Add --no-edit flag to skip the editor: git revert a1b2c3d --no-edit

Purpose

The git revert command is the safest method for undoing pushed commits. It does not rewrite history, which means collaborators who pulled your code will not face synchronization problems.

Step Three: How Do You Reset to a Previous Commit Using Git Reset?

To reset your branch, run git reset --hard <commit-hash> which moves the HEAD pointer and branch reference back to the specified commit, removing all subsequent commits from the branch history.

Action

  • Soft reset: git reset --soft HEAD~1 keeps changes staged
  • Mixed reset: git reset HEAD~1 keeps changes in working directory
  • Hard reset: git reset --hard HEAD~1 discards all changes completely

The HEAD~1 notation means “one commit before current HEAD.” Use HEAD~3 to go back three commits.

Purpose

Understanding what git reset does matters because it rewrites commit history.

Only use reset on commits that have not been pushed to a remote repository. For shared branches, stick with git revert instead.

Soft vs Hard Reset Comparison

Reset TypeHEAD (History)Index (Staging)Working DirectoryPrimary Use Case
--softMovedUnchangedUnchangedUndo a commit to fix the message or squash commits.
--mixedMovedResetUnchanged(Default) Unstage files to re-organize the next commit.
--hardMovedResetResetComplete rollback; discard all local work (Dangerous).

Step Four: How Do You Push Reverted Changes to GitHub?

To push your reverted changes, run git push origin <branch-name> which uploads the new revert commit to the remote branch on GitHub, making the rollback visible to all collaborators.

Action

  • Standard push: git push origin main
  • Force push after reset: git push origin main --force
  • Expected result: Terminal displays “branch updated” confirmation

Force push (--force) is required after git reset because the remote history differs from local. Use with caution on shared branches.

Purpose

Understanding what git push does clarifies why this step finalizes your revert. Local changes remain invisible to teammates until pushed to origin.

Step Five: How Do You Revert a Commit in GitHub Desktop?

To revert using GitHub Desktop, click History in the left sidebar, right-click the target commit, and select “Revert changes in commit” which creates a new commit that undoes the selected changes without command line interaction.

Action

  • Path: History tab > Right-click commit > Revert changes in commit
  • Confirm: Click “Revert” in the dialog box
  • Push: Click “Push origin” button in top bar

Revert multiple commits from newest to oldest to avoid merge conflicts.

Purpose

GitHub Desktop provides a visual interface for developers who prefer GUI over terminal. Same result as git revert, different workflow.

Verification

Confirm your revert worked by checking both the commit log and file contents.

Commands to Verify

  • git log --oneline -5: Shows last 5 commits; revert commit should appear at top
  • git status: Should display “nothing to commit, working tree clean”
  • git diff HEAD~1: Shows differences between current and previous commit

Check git status output. A clean working directory confirms successful completion.

Expected Results

Your git log displays a new entry starting with “Revert” followed by the original commit message.

File contents match the state before the problematic commit was made.

Troubleshooting

Issue: Merge Conflicts During Revert

Solution: Open conflicted files, look for conflict markers (<<<<<<<), manually choose which code to keep, then run git add . followed by git revert --continue.

Learn how to resolve merge conflicts in Git for detailed walkthrough.

Issue: Cannot Push After Reset

Solution: Remote branch has commits your local branch no longer has. Run git push origin main --force to overwrite remote history.

Warning: Force push erases remote commits permanently. Coordinate with your team first.

Issue: Reverted Wrong Commit

Solution: Revert the revert. Run git revert HEAD to undo your revert commit, which restores the original changes.

Check git reflog to see complete history including reverts and resets.

Issue: Detached HEAD State

Solution: You checked out a commit directly instead of a branch. Run git checkout main to reattach HEAD to your branch.

Understanding detached HEAD in Git prevents accidental data loss.

Git Revert vs Git Reset

Choosing between these commands depends on whether commits have been pushed and whether you need to preserve history.

FactorGit RevertGit Reset
History EffectPreserved; adds a new “inverse” commit.Rewritten; removes commits from the branch.
Public SafetySafe for pushed/public branches.Dangerous; requires a force-push.
CollaborationNo sync issues for teammates.Can break teammates’ local repos.
ExecutionCreates a new audit trail of the fix.Leaves no trace that the error occurred.
Undo ScopeCan target any commit in history.Best for undoing the most recent work.

When to Choose Each Method

Use git revert when: Commits are pushed, you work on shared branches, or you need audit trail for software compliance.

Use git reset when: Commits are local only, you want cleaner history, or you made mistakes before pushing.

Related Processes

After mastering commit reversion, explore these connected version control operations.

Consider implementing continuous integration to catch bugs before they require manual rollbacks.

A proper Git workflow reduces how often you need to revert commits in the first place.

FAQ on How To Revert To A Previous Commit In Github

What is the difference between git revert and git reset?

Git revert creates a new commit that undoes changes while preserving history. Git reset moves the branch pointer backward and can erase commits entirely.

Use revert for pushed commits; use reset for local changes only.

Can I revert a commit that was already pushed to GitHub?

Yes. Run git revert <commit-hash> then push to GitHub normally.

This creates a new commit that cancels the unwanted changes without rewriting remote history or affecting collaborators.

How do I find the commit hash I need to revert to?

Run git log --oneline to display your commit history with shortened hashes. Each entry shows a 7-character identifier followed by the commit message.

Copy the hash of your target commit.

Will git reset delete my code permanently?

With --hard flag, yes. Hard reset erases uncommitted changes from your working directory permanently.

Use --soft to keep changes staged, or check what git stash does to save work temporarily.

How do I revert multiple commits at once?

Revert commits individually from newest to oldest: git revert HEAD~2..HEAD reverts the last two commits.

Alternatively, reset to a specific commit with git reset --hard <hash> if commits are unpushed.

What happens if I get merge conflicts during revert?

Git pauses the revert process. Open conflicted files, resolve the conflict markers manually, run git add . then git revert --continue.

The revert completes after conflict resolution.

Can I undo a git revert if I made a mistake?

Yes. Run git revert HEAD to revert your revert commit, which restores original changes.

Check git reflog to see complete history and recover any lost commits if needed.

Do I need to force push after using git reset?

Yes, if commits were already pushed. Run git push origin main --force to overwrite remote branch history.

Coordinate with teammates first since force push affects everyone’s local repositories.

How do I revert a commit in GitHub Desktop without command line?

Click History tab, right-click the target commit, select “Revert changes in commit.”

GitHub Desktop creates a revert commit automatically. Click “Push origin” to sync with your GitHub repository.

Is there a way to preview changes before reverting?

Run git show <commit-hash> to view exact changes in that commit. Use git diff to compare versions.

This helps confirm you are reverting the correct commit before execution.

Conclusion

Mastering how to revert to a previous commit in GitHub gives you control over your project’s version history and protects your codebase from broken deployments.

You now have multiple approaches: git revert for safe rollbacks on shared branches, git reset for local cleanup, and GitHub Desktop for visual workflows.

The right choice depends on whether commits are pushed and how your team handles source control management.

Practice these commands on a test branch first. Mistakes happen less when the workflow becomes muscle memory.

Keep git reflog` in mind as your safety net. It tracks every HEAD movement, letting you recover from almost any Git mistake.

Strong branch habits and regular commits make future reverts simpler and less stressful.

50218a090dd169a5399b03ee399b27df17d94bb940d98ae3f8daff6c978743c5?s=250&d=mm&r=g How to Revert to a Previous Commit in GitHub

Stay sharp. Ship better code.

Every week: one curated article, one tool worth knowing, one tip you can use tomorrow. No noise, no padding.