Git Revert to Previous Commit: Reset, Checkout, or Rollback (Examples)

Git Revert to Previous Commit: Reset, Checkout, or Rollback (Examples)

Reverting to a previous commit in Git allows you to restore your project to an earlier state when something breaks or incorrect changes are introduced. Depending on the situation, you may want to temporarily view an older commit, reset your branch to a specific commit, or safely undo changes using git revert. This guide explains the most common ways to go back to a previous commit with practical examples and quick reference commands.


Git Revert to Previous Commit – Quick Command Cheat Sheet

CommandDescription
git checkout <commit-hash>Switches to an earlier commit in detached HEAD mode without changing branch history.
git checkout -b <branch> <commit-hash>Creates a new branch starting from a specific commit.
git reset --hard <commit-hash>Moves branch pointer to the specified commit and deletes newer commits.
git reset --soft <commit-hash>Removes commits but keeps all changes staged for recommitting.
git reset --mixed <commit-hash>Removes commits and unstages files while keeping changes locally.
git reset --hard HEAD~1Deletes the latest commit and all associated changes.
git reset --soft HEAD~1Removes the commit but keeps files staged.
git revert <commit-hash>Creates a new commit that reverses the changes of a previous commit.
git revert <old-commit>..<new-commit>Reverts a range of commits in sequence.
git checkout <commit-hash> -- <file>Restores a specific file from an earlier commit.
git checkout <commit-hash> -- <file>Recovers a deleted file from an earlier commit snapshot.
git revert -m 1 <merge-commit>Reverts a merge commit by selecting the parent branch.
git revert HEADReverts the latest pushed commit without rewriting history.
git reset --hard <commit-hash>Rolls back the branch to the specified commit and removes newer commits.
git reflogDisplays commit history references that allow recovery of lost commits.
git reset --hard <commit-hash>Restores repository state using a commit found in git reflog.

Understanding "Revert to Previous Commit" in Git

Reverting to a previous commit in Git means restoring your repository to an earlier state in the commit history. This is commonly done when a recent change introduces a bug, incorrect configuration, or unwanted modifications to the project. Git provides multiple ways to move back to a previous commit depending on whether you want to temporarily inspect the state, permanently change history, or safely undo changes.

Before choosing a command, it is important to understand how reverting works and how it affects the repository history.

What happens when you revert a commit

When you revert a commit, Git creates a new commit that reverses the changes introduced by the selected commit. Instead of deleting history, Git keeps the original commit and adds another commit that cancels its effect.

For example, assume the commit history looks like this:

text
A → B → C

If commit C introduced a bug, running:

bash
git revert C

creates a new commit that undoes the changes from commit C:

text
A → B → C → D

Here commit D reverses the changes made in commit C, while keeping the history intact.

This approach is considered safe for shared repositories because it does not rewrite history.

Difference between reverting and resetting history

Git provides two commonly used commands to go back to a previous commit:

CommandBehaviorHistory Impact
git revertCreates a new commit that reverses earlier changesPreserves history
git resetMoves the branch pointer to an earlier commitRewrites history

Example using reset:

bash
git reset --hard HEAD~1

If the history was:

text
A → B → C → D

After the reset command:

text
A → B → C

Commit D is removed from the branch history.

Because reset rewrites history, it should be used carefully when working with shared repositories.

When reverting is safer than rewriting history

Using git revert is safer in the following situations:

  • The commit has already been pushed to a remote repository
  • Multiple developers are working on the same branch
  • The commit history should remain unchanged for auditing purposes
  • You want to undo changes without disrupting other contributors

In contrast, git reset is typically used when:

  • the commits exist only in your local repository
  • you want to clean up commit history before pushing
  • you need to remove incorrect commits entirely

Temporarily Go Back to a Previous Commit

Sometimes developers need to inspect an earlier version of the project without permanently changing commit history. Git allows this by checking out a specific commit.

Using git checkout to view a previous commit

To switch to a previous commit temporarily, use:

bash
git checkout <commit-hash>

Example:

bash
git checkout a1b2c3

This command places your repository into detached HEAD mode, allowing you to explore or test the project as it existed at that commit.

You can verify the commit history using:

bash
git log --oneline

This is useful when debugging issues or reviewing past changes.

Creating a branch from a previous commit

If you want to continue development from an earlier commit, you can create a new branch from that commit.

bash
git checkout -b new-branch <commit-hash>

Example:

bash
git checkout -b hotfix a1b2c3

This creates a new branch called hotfix starting from the selected commit.

This method is often used when:

  • restoring a stable version of code
  • creating a hotfix from a previous release
  • testing older versions of a project

Detached HEAD explained

When checking out a specific commit instead of a branch, Git enters detached HEAD mode.

In this state:

  • HEAD points directly to a commit instead of a branch
  • new commits are not attached to any branch
  • switching branches may discard those commits

Example commit structure:

text
A → B → C → D (main)

After checking out commit B:

text
HEAD → B

You are now exploring the repository at commit B without modifying the main branch.

To resume normal development, switch back to a branch:

bash
git checkout main

Reset Branch to a Previous Commit

Resetting a branch allows you to move the branch pointer to an earlier commit and optionally remove commits after that point.

Resetting the latest commit

If you want to remove the most recent commit:

bash
git reset --hard HEAD~1

This command moves the branch pointer one commit backward.

Before reset:

text
A → B → C → D (HEAD)

After reset:

text
A → B → C (HEAD)

Commit D is removed.

Resetting to a specific commit hash

You can also reset the branch to a specific commit.

bash
git reset --hard <commit-hash>

Example:

bash
git reset --hard a1b2c3

This command removes all commits that occurred after the specified commit.

Resetting multiple commits at once

To remove several commits simultaneously, you can reset the branch multiple commits backward.

Example removing the last three commits:

bash
git reset --hard HEAD~3

If the history looked like:

text
A → B → C → D → E

After the reset:

text
A → B

Commits C, D, and E are removed.

Reset types explained

Git reset has three primary modes that determine how changes are handled.

git reset --soft

bash
git reset --soft HEAD~1

Behavior:

  • removes the commit
  • keeps all changes staged
  • allows you to recommit immediately

Useful when you want to edit or combine commits.

git reset --mixed

bash
git reset --mixed HEAD~1

Behavior:

  • removes the commit
  • unstages the files
  • keeps changes in the working directory

This is the default reset mode.

git reset --hard

bash
git reset --hard HEAD~1

Behavior:

  • removes the commit
  • deletes staged changes
  • discards working directory changes

⚠️ Use this carefully because the changes are permanently deleted.


Safely Undo a Previous Commit Using Git Revert

When commits have already been pushed to a shared repository, using git revert is the safest method to undo changes.

Reverting a single commit

To revert a specific commit:

bash
git revert <commit-hash>

Example:

bash
git revert a1b2c3

Git creates a new commit that reverses the changes introduced in the selected commit.

Reverting multiple commits

You can revert several commits individually by running revert multiple times.

bash
git revert <commit1>
git revert <commit2>

Each command generates a separate commit that reverses the respective change.

Reverting a range of commits

Git also allows reverting a range of commits using the following syntax:

bash
git revert <old-commit>..<new-commit>

Example:

bash
git revert a1b2c3..d4e5f6

This command reverts all commits between the two specified commit hashes.

This approach is useful when multiple commits introduced issues and you want to undo them without rewriting commit history.


Rollback Repository to a Previous Commit

Sometimes you may want to roll back the entire repository to an earlier commit when recent changes introduce bugs, configuration issues, or unstable code. In such cases, Git allows you to move the branch pointer to an earlier commit and discard all commits that occurred afterward.

Reset branch to a specific commit

To move your branch back to a specific commit, use:

bash
git reset --hard <commit-hash>

Example:

bash
git reset --hard a1b2c3

If the commit history looked like this:

text
A → B → C → D → E (HEAD)

After resetting to commit C:

text
A → B → C (HEAD)

Commits D and E are removed from the branch.

This method is commonly used when you want to restore the repository to a known stable version.

Remove commits after a specific commit

If multiple commits were introduced after a stable commit, you can remove all of them using reset.

Example removing the last four commits:

bash
git reset --hard HEAD~4

This moves the branch pointer four commits backward.

Alternatively, resetting to a commit hash removes everything after that commit:

bash
git reset --hard <commit-hash>

This is useful when you want to roll back several changes at once.

Revert entire history after a commit

Instead of deleting commits, you may want to undo all changes after a certain commit while preserving history.

You can revert a range of commits:

bash
git revert <commit>..HEAD

Example:

bash
git revert a1b2c3..HEAD

This creates new commits that undo all changes introduced after commit a1b2c3.

Before revert:

text
A → B → C → D → E

After revert:

text
A → B → C → D → E → F → G

Commits F and G undo the changes introduced in commits D and E.

This approach is safer for shared repositories.


Restore Files from a Previous Commit

Sometimes you do not want to revert the entire repository. Instead, you may want to restore specific files from a previous commit.

Git allows you to recover files directly from the commit history.

Restore a single file from a previous commit

To restore a specific file from an earlier commit:

bash
git checkout <commit-hash> -- <file>

Example:

bash
git checkout a1b2c3 -- config.yml

This command replaces the current version of config.yml with the version stored in the specified commit.

After restoring the file, you can commit the change.

Restore multiple files

You can restore multiple files from a previous commit in the same way:

bash
git checkout <commit-hash> -- file1 file2 file3

Example:

bash
git checkout a1b2c3 -- index.html style.css script.js

This restores all specified files from the selected commit.

Restore deleted files

If a file was accidentally deleted, you can recover it from the previous commit.

First find the commit containing the file:

bash
git log -- <file>

Then restore the file:

bash
git checkout <commit-hash> -- <file>

This recreates the file from the repository history.


Revert to Previous Commit After Push

Reverting commits becomes more sensitive when the changes have already been pushed to a remote repository.

In collaborative environments, rewriting history may disrupt other developers' work.

Safely undo pushed commits

The safest method to undo a pushed commit is using git revert.

bash
git revert <commit-hash>

Example:

bash
git revert HEAD

Instead of removing the commit, Git creates a new commit that reverses the previous changes.

Before revert:

text
A → B → C

After revert:

text
A → B → C → D

Commit D cancels the changes from commit C.

When to avoid git reset after push

Avoid using:

bash
git reset --hard

after commits have been pushed.

Reset rewrites history, which can cause:

  • merge conflicts for other developers
  • loss of commit history
  • broken branches in remote repositories

If history must be rewritten, a force push is required:

bash
git push --force

However, this should only be done with caution.

Using git revert to preserve commit history

Because git revert creates a new commit instead of removing history, it is the preferred approach for shared repositories.

Benefits of using revert:

  • history remains intact
  • other developers are not affected
  • easier auditing of changes

Revert a Merge Commit

Merge commits behave differently from regular commits because they contain changes from multiple parent branches.

Reverting a merge commit requires specifying which parent branch should remain.

Why reverting merge commits is different

A merge commit has two parent commits, representing the branches that were merged.

Example history:

text
A → B → C
        D → E

If commit E merges another branch into main, reverting it requires choosing the correct parent.

Using git revert -m

To revert a merge commit:

bash
git revert -m 1 <merge-commit-hash>

Example:

bash
git revert -m 1 a1b2c3

The -m option specifies the parent branch to keep.

Choosing the correct parent branch

Parent numbers represent the branches involved in the merge:

ParentMeaning
-m 1Keep the main branch changes
-m 2Keep the merged branch changes

Choosing the correct parent ensures that the repository returns to the intended state.


Recover After Reset or Revert

Even if commits are removed or reset, Git often keeps references that allow you to recover them.

Recover commits using git reflog

git reflog tracks changes to the HEAD pointer and stores references to previous commits.

Run:

bash
git reflog

Example output:

text
a1b2c3 HEAD@{0}: reset: moving to HEAD~1
d4e5f6 HEAD@{1}: commit: add feature

This allows you to locate commits that were removed.

Restore lost commits after reset

Once you find the commit hash in the reflog, you can restore it using:

bash
git reset --hard <commit-hash>

This moves the repository back to that commit.

Recover commits after force push

If commits were lost after a force push, recovery may still be possible using:

bash
git reflog

If the commit exists locally, it can be restored using reset or by creating a new branch:

bash
git checkout -b recovered-branch <commit-hash>

This recreates a branch starting from the recovered commit.


Git Reset vs Git Revert vs Git Checkout

Git provides several commands to go back to a previous commit or undo changes. The most commonly used commands are git reset, git revert, and git checkout. Although they may appear similar, they behave differently and affect commit history in different ways.

Understanding when to use each command is important to avoid losing commits or breaking collaboration with other developers.

Key differences between reset and revert

The main difference between git reset and git revert is how they handle commit history.

CommandPurposeHistory ImpactRecommended Use
git resetMoves the branch pointer to a previous commitRewrites historyLocal commits not pushed yet
git revertCreates a new commit that reverses previous changesPreserves historyShared or pushed repositories
git checkoutSwitches branches or views previous commitsNo history changeInspect older commits

Example using reset:

bash
git reset --hard HEAD~1

This removes the most recent commit and moves the branch pointer backward.

Example using revert:

bash
git revert HEAD

This creates a new commit that reverses the changes from the previous commit without deleting history.

When to use checkout instead of reset

If you simply want to inspect a previous commit without modifying history, use git checkout.

Example:

bash
git checkout <commit-hash>

This command places the repository in detached HEAD mode, allowing you to explore the project as it existed at that point in time.

You can also create a branch from that commit:

bash
git checkout -b new-branch <commit-hash>

This approach is useful when:

  • debugging older versions of code
  • testing previous releases
  • creating a branch from a stable commit

When to use restore command

Newer versions of Git introduced the git restore command to simplify file recovery from previous commits.

To restore a file from a previous commit:

bash
git restore --source=<commit-hash> <file>

Example:

bash
git restore --source=a1b2c3 config.yml

This restores the file version stored in that commit.

git restore is often used when:

  • recovering accidentally modified files
  • restoring deleted files
  • undoing local file changes without affecting commit history

Common Mistakes When Reverting Commits

When reverting commits, developers sometimes make mistakes that lead to lost work or broken repository history. Understanding these common issues can help you avoid serious problems.

Losing commits with git reset --hard

The command:

bash
git reset --hard

removes commits and deletes all changes from the working directory.

If the changes are not saved elsewhere, they may be permanently lost.

Before using a hard reset, it is a good practice to create a backup branch:

bash
git branch backup-before-reset

This ensures that you can recover commits if needed.

Reverting commits on shared branches

Using git reset on branches that are shared with other developers can cause serious issues.

For example:

bash
git reset --hard HEAD~1
git push --force

This rewrites commit history on the remote repository.

Other developers may experience:

  • merge conflicts
  • missing commits
  • broken branches

When working on shared repositories, prefer using:

bash
git revert <commit-hash>

because it preserves commit history.

Resetting the wrong branch

Another common mistake is resetting commits on the wrong branch.

Before running destructive commands, always verify the active branch:

bash
git branch

or

bash
git status

This helps prevent accidental changes to important branches like main or production.


Frequently Asked Questions

1. How do I revert to a previous commit in Git?

You can revert to a previous commit using git revert <commit-hash> to safely undo changes, or git reset --hard <commit-hash> to move the branch pointer to that commit and remove later commits.

2. How do I go back to a previous commit without losing history?

Use git revert <commit-hash> to undo the changes from a previous commit while keeping the commit history intact.

3. How do I checkout a previous commit in Git?

You can temporarily view a previous commit using git checkout <commit-hash>. This places the repository in detached HEAD mode so you can inspect the project at that commit.

4. How do I rollback multiple commits in Git?

You can rollback multiple commits using git reset --hard HEAD~N to remove the last N commits or use git revert <commit>..HEAD to safely undo them with new commits.

5. Can I recover a commit after resetting Git history?

Yes. You can recover lost commits using git reflog, which shows previous commit references that can be restored using git reset --hard <commit-hash>.

Summary

Reverting to a previous commit is a common task when working with Git, especially when recent changes introduce bugs or unexpected behavior. Git provides multiple commands to handle this situation depending on whether you want to temporarily inspect a previous commit, permanently remove commits, or safely undo changes without rewriting history.

In this guide, we explored different ways to revert to previous commits using commands such as git checkout, git reset, and git revert. We also covered practical scenarios including rolling back commits, restoring files from history, safely undoing pushed commits, reverting merge commits, and recovering lost commits using git reflog.

Understanding how these commands affect commit history helps developers safely manage version control while avoiding common mistakes that could result in lost work or broken repositories.


Official Documentation

For more information about reverting commits and managing Git history, refer to the official Git documentation:

Steve Alila

Steve Alila

Specializes in web design, WordPress development, and data analysis, with proficiency in Python, JavaScript, and data extraction tools. Additionally, he excels in web API development, AI integration, and data presentation using Matplotlib and Plotly.