A question that comes frequently with developers is how to Git Reset to Previous Commit. Version control is an essential aspect of software development, and Git is one of the most popular tools for managing code changes.
When working with Git, sometimes you need to undo changes and go back to a previous commit. Whether you made a mistake, committed the wrong files, or need to revert changes, Git provides several ways to Git Reset to Previous Commit.
In this guide, we’ll explore how to Git Reset to Previous Commit and cover various related scenarios.
What is Git Reset?

The Git reset command is used to undo changes by moving the HEAD of a branch to a specific commit. It modifies the commit history and can alter the working directory, the staging area, or both, depending on the reset mode used. This makes it a vital tool for correcting errors or cleaning up a repository.
Modes of Git Reset
Before using Git Reset to Previous Commit, it’s crucial to understand its three modes:
Soft Reset

Explanation:
- Command:
git reset --soft <commit-hash>
- What it does: Moves the branch’s HEAD to the specified commit while keeping the changes in the staging area.
- Use case: Ideal when revising a commit or combining multiple changes into a single commit.
Mixed Reset (Default Mode)

Explanation:
- Command:
git reset --mixed <commit-hash>
- What it does:
Moves the branch’s HEAD to the specified commit, removes changes from the staging area, but retains them in the working directory. - Use case:
Useful when you want to unstage changes but keep your edits for further modification.
Hard Reset

Explanation:
- Command:
git reset --hard <commit-hash>
- What it does:
Moves the branch’s HEAD to the specified commit and discards all changes in:- Staging area
- Working directory
- Caution:
- This operation permanently deletes all uncommitted changes, so use it carefully to avoid data loss.
How to Find the Commit Hash

To Git reset to a previous commit, you need its hash. Follow these steps:
- Run
git log
to view the commit history. - Locate the desired commit and copy its hash (the alphanumeric string at the beginning of each log entry).
Example:
bashCopyEditgit log --oneline
Output:
sqlCopyEdit9fceb02 Fix bug in user authentication
ae45a8d Add login functionality
b7e23c5 Initial commit
To reset to ae45a8d, use the respective command.
Understanding Git Reset and Git Revert
Before diving into the commands, it’s important to understand the difference between Git Reset and Git Revert:
Git Revert creates a new commit that undoes changes from a previous commit, keeping the history intact.
Git Reset moves the HEAD (current branch) to a previous commit, modifying your commit history.
Resetting a File to a Previous Commit
Sometimes, you may want to reset only a single file to a previous state without affecting the rest of the repository. Here’s how:
Command to Reset a Single File:
bashCopyEditgit checkout <commit-hash> -- <file-path>
Result: Replaces the file in your working directory with its state in the specified commit.
Example:
bashCopyEditgit checkout ae45a8d -- src/app.js
This command can also be used to revert changes in a specific file without impacting others. For example, if you want to reset app.js
but keep other files unchanged, this method works seamlessly.
git reset
vs. git revert
: Which One Should You Use?
Feature | git reset | git revert |
---|---|---|
Modifies history? | Yes | No |
Deletes commits? | Yes | No (creates a new commit) |
Safe for team collaboration? | No | Yes |
Restores working directory? | Yes | No |
Git Revert

While git reset modifies the commit history, git revert creates a new commit that undoes the changes of a previous commit. Use git revert when collaborating with others to avoid rewriting shared commit history.
To revert a file to its previous state without resetting the repository, you can use:
bashCopyEditgit revert <commit-hash>
This command creates a new commit to undo the effects of the specified commit while preserving the project’s history.
Best Practices for Using Git Reset
- Backup Your Repository: Before using
--hard
, ensure your changes are backed up. - Work on Local Branches: Avoid using git reset on shared branches to prevent conflicts.
- Understand the Impact: Choose the reset mode based on your goal (soft, mixed, or hard).
- Use Git Log: Familiarize yourself with commit hashes and their significance.
- Experiment on Test Repos: Practice using git reset on a test repository to build confidence.
Git Reset for Remote Repositories
Resetting a remote repository to a previous commit requires force-pushing changes. Use caution, as this can overwrite changes made by others.
Steps to Reset Remote Repository:
- Reset your local branch:bashCopyEdit
git reset --hard <commit-hash>
- Force push the changes:bashCopyEdit
git push origin <branch-name> --force
Important: Inform your team before resetting a remote branch to prevent conflicts.
When to Use Each Reset Mode
Scenario | Recommended Command |
Undo last commit but keep changes staged | git reset --soft HEAD~1 |
Undo last commit and unstage changes | git reset --mixed HEAD~1 |
Completely discard last commit | git reset --hard HEAD~1 |
Undo changes to a specific file | git checkout <commit-hash> -- <file-path> |
Undo changes without modifying commit history | git revert <commit-hash> |
Conclusion
Mastering git reset can significantly enhance your Git workflow, giving you the ability to clean up your repository and refine your commit history.
Git provides multiple ways to Git Reset to Previous Commit based on your needs. Use git reset
to modify history when working locally and git revert
to undo changes safely when collaborating.
Understanding these commands will help you manage your commits effectively and avoid unnecessary issues.
FAQs
How to Reset to a Previous Commit in Git?
Use git reset --hard <commit-hash>
to reset completely or git reset --soft <commit-hash>
to keep changes staged.
How to Revert to a Previous Commit Without Losing Changes?
Use git reset --soft <commit-hash>
to move the HEAD without deleting changes.
How to Undo the Last Commit in Git?
Use git reset --soft HEAD~1
to keep changes or git reset --hard HEAD~1
to discard them.
How to Revert a Commit After Pushing?
Use git revert <commit-hash>
to create a new commit that undoes the changes, then push.
How to Reset a Single File to a Previous Commit?
Use git checkout <commit-hash> -- <file-path>
to restore a file to an earlier version.
How to Remove the Last Commit from History?
Use git reset --hard HEAD~1
followed by git push --force
(use with caution).
How to Undo Staged Changes in Git?
Use git reset HEAD <file-path>
to move a file from the staging area back to the working directory.
How to Revert to a Previous Commit in a Branch?
Usegit reset --hard <commit-hash>
to reset orgit revert <commit-hash>
to keep history intact.