When working with Git, mistakes happen. You may accidentally introduce a bug, commit experimental code, or need to undo a change that has already been pushed to a shared repository.
Two commands commonly used for this are git revert and git reset.
Although both can be used to undo changes, they work very differently. Choosing the wrong one—especially on a shared branch—can create unnecessary problems for your team.
Git Revert: The Safe Choice for Shared Branches
git revert creates a new commit that reverses the changes introduced by an earlier commit.
For example:
git log --oneline
Suppose you identify a commit that introduced an unwanted change:
x9y8z7w Add experimental dashboard widget
You can reverse it with:
git revert x9y8z7w
Git creates a new commit that effectively undoes the changes from the selected commit.
The important part is that the original commit remains in the repository history.
This makes git revert especially useful when working with shared branches such as main.
Git Reset: Moving the Branch Backward
git reset works differently.
Instead of creating a new commit, reset moves the current branch pointer to another commit.
For example:
git reset --hard HEAD~1
This can remove the latest commit from the current branch history.
The --hard option can also discard working-directory changes, so it should be used carefully.
Reset can be useful when working on your own local branch and you intentionally want to rewrite your local history.
However, using history-rewriting commands on a branch that other developers are already using can cause synchronization problems.
Revert vs Reset
The simplest way to remember the difference is:
Git revert = create a new commit that undoes an old commit.
Git reset = move the branch pointer backward.
If a commit has already been pushed to a shared branch, git revert is generally the safer approach because it preserves the existing history.
What About Merge Conflicts?
A revert can sometimes produce merge conflicts.
This can happen when later commits have changed the same parts of the code that the original commit modified.
If Git stops because of a conflict, inspect the affected files and resolve the conflicting changes.
Then stage the resolved files:
git add
Continue the revert:
git revert --continue
If you decide that you don't want to continue with the revert, you can abort it:
git revert --abort
Which Command Should You Use?
Use git revert when:
The commit has already been pushed.
You are working on a shared branch.
You want to preserve the project history.
Other developers may already have the commit.
Use git reset when:
You are working on your own local branch.
You intentionally want to rewrite local history.
You understand the consequences of moving the branch pointer.
The key is not that one command is always better than the other. They solve different problems.
Final Takeaway
If you need to undo a change on a shared repository, git revert is usually the safer approach because it adds a new commit instead of removing existing history.
If you're working locally and need to reorganize or remove commits before sharing your branch, git reset can be useful—but commands such as git reset --hard should be used carefully.
For a complete step-by-step walkthrough of safely reverting a Git commit, including handling conflicts and reverting pushed changes, see the detailed guide on TechArticlesHub.
Read the complete Git revert guide:
Top comments (0)