In the world of version control, mistakes are a natural part of the development process. Understanding how to undo changes effectively is crucial for maintaining a clean project history. Two common commands for undoing changes in Git are git reset
and git revert
. While both serve the purpose of undoing changes, they operate in fundamentally different ways. This blog will clarify the differences between these two commands and when to use each one.
1. What Is Git Reset?
Git Reset is a command used to undo changes by resetting the current HEAD to a specified state. It modifies the commit history by moving the branch pointer backward and can affect the staging area and working directory.

Types of Git Reset
- Soft Reset (
--soft
): Moves HEAD to a specified commit but leaves your changes in the staging area.
git reset --soft <commit>
- Mixed Reset (default): Moves HEAD to a specified commit and unstages the changes, leaving them in the working directory.
git reset <commit>
- Hard Reset (
--hard
): Moves HEAD to a specified commit and discards all changes in the working directory and staging area.
git reset --hard <commit>
Use Cases for Git Reset
- You want to undo the last few commits and continue working without those changes.
- You accidentally staged files and want to unstage them.
- You need to discard all changes and return to a clean state.
2. What Is Git Revert?
Git Revert is a command that creates a new commit to undo the changes made by a previous commit. Instead of modifying the commit history, it appends a new commit that effectively reverses the changes.

Using Git Revert
To revert a specific commit, use:
git revert <commit>
This command creates a new commit that undoes the changes from the specified commit.
Use Cases for Git Revert
- You want to keep the history intact but remove the effects of a specific commit.
- You’re working in a shared repository and need to undo changes without altering the commit history.
3. Key Differences Between Git Reset and Git Revert
Feature | Git Reset | Git Revert |
---|---|---|
Impact on History | Alters commit history | Preserves commit history |
Type of Changes | Moves HEAD and can discard changes | Creates a new commit that undoes changes |
Use in Collaboration | Risky in shared repositories | Safe for collaboration |
Flexibility | Offers different modes (soft, mixed, hard) | Simple and straightforward |
4. When to Use Each Command
- Use
git reset
when: - You are working in a personal branch.
- You need to completely discard changes and don’t mind rewriting history.
- Use
git revert
when: - You’re working in a shared branch or repository.
- You want to keep a clear and accurate history of changes.
5. Real-World Examples
Example of Git Reset:
Suppose you accidentally committed changes that you want to discard. You can use:
git reset --hard HEAD~1
This command will remove the last commit and all associated changes.
Example of Git Revert:
Imagine you deployed a feature that caused an issue. Instead of resetting, you would use:
git revert <commit_hash>
This creates a new commit that undoes the problematic changes, preserving the history.
6. Conclusion
Both git reset
and git revert
are powerful tools for undoing changes in Git, but they serve different purposes. Understanding when to use each command will help you maintain a clean project history while effectively managing your codebase. By using these commands wisely, you can navigate through mistakes and ensure a smooth development process.