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
| Command | Description |
|---|---|
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~1 | Deletes the latest commit and all associated changes. |
git reset --soft HEAD~1 | Removes 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 HEAD | Reverts 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 reflog | Displays 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:
A → B → CIf commit C introduced a bug, running:
git revert Ccreates a new commit that undoes the changes from commit C:
A → B → C → DHere 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:
| Command | Behavior | History Impact |
|---|---|---|
git revert | Creates a new commit that reverses earlier changes | Preserves history |
git reset | Moves the branch pointer to an earlier commit | Rewrites history |
Example using reset:
git reset --hard HEAD~1If the history was:
A → B → C → DAfter the reset command:
A → B → CCommit 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:
git checkout <commit-hash>Example:
git checkout a1b2c3This 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:
git log --onelineThis 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.
git checkout -b new-branch <commit-hash>Example:
git checkout -b hotfix a1b2c3This 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:
A → B → C → D (main)After checking out commit B:
HEAD → BYou are now exploring the repository at commit B without modifying the main branch.
To resume normal development, switch back to a branch:
git checkout mainReset 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:
git reset --hard HEAD~1This command moves the branch pointer one commit backward.
Before reset:
A → B → C → D (HEAD)After reset:
A → B → C (HEAD)Commit D is removed.
Resetting to a specific commit hash
You can also reset the branch to a specific commit.
git reset --hard <commit-hash>Example:
git reset --hard a1b2c3This 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:
git reset --hard HEAD~3If the history looked like:
A → B → C → D → EAfter the reset:
A → BCommits C, D, and E are removed.
Reset types explained
Git reset has three primary modes that determine how changes are handled.
git reset --soft
git reset --soft HEAD~1Behavior:
- removes the commit
- keeps all changes staged
- allows you to recommit immediately
Useful when you want to edit or combine commits.
git reset --mixed
git reset --mixed HEAD~1Behavior:
- removes the commit
- unstages the files
- keeps changes in the working directory
This is the default reset mode.
git reset --hard
git reset --hard HEAD~1Behavior:
- 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:
git revert <commit-hash>Example:
git revert a1b2c3Git 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.
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:
git revert <old-commit>..<new-commit>Example:
git revert a1b2c3..d4e5f6This 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:
git reset --hard <commit-hash>Example:
git reset --hard a1b2c3If the commit history looked like this:
A → B → C → D → E (HEAD)After resetting to commit C:
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:
git reset --hard HEAD~4This moves the branch pointer four commits backward.
Alternatively, resetting to a commit hash removes everything after that commit:
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:
git revert <commit>..HEADExample:
git revert a1b2c3..HEADThis creates new commits that undo all changes introduced after commit a1b2c3.
Before revert:
A → B → C → D → EAfter revert:
A → B → C → D → E → F → GCommits 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:
git checkout <commit-hash> -- <file>Example:
git checkout a1b2c3 -- config.ymlThis 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:
git checkout <commit-hash> -- file1 file2 file3Example:
git checkout a1b2c3 -- index.html style.css script.jsThis 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:
git log -- <file>Then restore the file:
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.
git revert <commit-hash>Example:
git revert HEADInstead of removing the commit, Git creates a new commit that reverses the previous changes.
Before revert:
A → B → CAfter revert:
A → B → C → DCommit D cancels the changes from commit C.
When to avoid git reset after push
Avoid using:
git reset --hardafter 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:
git push --forceHowever, 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:
A → B → C
↘
D → EIf commit E merges another branch into main, reverting it requires choosing the correct parent.
Using git revert -m
To revert a merge commit:
git revert -m 1 <merge-commit-hash>Example:
git revert -m 1 a1b2c3The -m option specifies the parent branch to keep.
Choosing the correct parent branch
Parent numbers represent the branches involved in the merge:
| Parent | Meaning |
|---|---|
-m 1 | Keep the main branch changes |
-m 2 | Keep 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:
git reflogExample output:
a1b2c3 HEAD@{0}: reset: moving to HEAD~1
d4e5f6 HEAD@{1}: commit: add featureThis 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:
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:
git reflogIf the commit exists locally, it can be restored using reset or by creating a new branch:
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.
| Command | Purpose | History Impact | Recommended Use |
|---|---|---|---|
git reset | Moves the branch pointer to a previous commit | Rewrites history | Local commits not pushed yet |
git revert | Creates a new commit that reverses previous changes | Preserves history | Shared or pushed repositories |
git checkout | Switches branches or views previous commits | No history change | Inspect older commits |
Example using reset:
git reset --hard HEAD~1This removes the most recent commit and moves the branch pointer backward.
Example using revert:
git revert HEADThis 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:
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:
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:
git restore --source=<commit-hash> <file>Example:
git restore --source=a1b2c3 config.ymlThis 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:
git reset --hardremoves 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:
git branch backup-before-resetThis 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:
git reset --hard HEAD~1
git push --forceThis rewrites commit history on the remote repository.
Other developers may experience:
- merge conflicts
- missing commits
- broken branches
When working on shared repositories, prefer using:
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:
git branchor
git statusThis 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 usinggit 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?
Usegit 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 usinggit 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 usinggit 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 usinggit 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:



![Git Error: Cannot Delete Branch Checked Out or Used by Worktree [SOLVED]](/cannot-delete-branch-checked-out-at/git-cannot-delete-branch_hu_3f86bd25a59d627d.webp)






