How To Revert A Commit In Git?

How To Revert A Commit In Git?

Using Git to its full potential is crucial when it comes to managing the history of your project. Reverting a commit in Git may depend on the changes you want to revert, but whether you need to revert your latest commit or multiple recent commits, there are several ways to do so. In this guide, we’ll show you not just how to revert a commit in Git to master, but also how to undo changes in a Git repository and even rollback a commit after it has been pushed to the remote repository. We’ll show some simple commands that clarify how to return to a particular commit in Git, which will help ensure that your codebase is neat and organized as well as clean and stable, even if you’ve made changes you don’t want in its past.

Whether you’re working on a project with other people or just using your repo as a solo workspace, knowing how to revert your commits gives you the confidence/knowledge to fix those mistakes without tearing down and rebuilding that house. This knowledge is especially helpful for teams working on custom software development, as it allows them to maintain clean version histories and deliver high-quality code. So, let’s take a look at the safest methods for undoing your work and keeping your master branch and project as a whole on target. 

What Is Git Revert?

Git revert is often compared with an ordinary “undo” operation; it bears some important differences because undoing a change undoes the very changes in the project’s history, which may cause some work to be undone. It does a new commit with patches that reverse changes in the original commit, so your project history remains clean. This is absolutely invaluable for the times when you try to fiddle around with some bugs or changes that you never meant to do, and if things don’t magically work, you can undo a commit instead of having to go through and manually edit files, or interrupt someone else’s flow. Unlike a command such as reset hard, git revert doesn’t overwrite history, so you don’t lose changes or past work, and your team’s commit history stays intact, killing two birds with one stone when it comes to collaborating on development.

What Is Git Commit?

In Git, a commit is like capturing a snapshot of your project at a given point in time. Each commit records the changes made to the codebase, paired with a descriptive message that explains the update, and is tagged with a unique commit hash for easy tracking. This makes commits essential for version control, enabling developers to document progress, roll back when needed, and maintain a clear, traceable project history that supports collaboration and long-term code integrity.

Step-by-Step Guide To Revert A Git Commit

Reverting a commit in Git is useful when you need to undo specific changes without rewriting history. Instead of deleting past work, Git creates a new commit that reverses the effect of the chosen one. This keeps your repository history clean and safe for collaboration.

Step 1: Identify the Commit Hash

Every commit in Git is tracked with a unique commit hash. To find the one you want to revert, use:

This command displays a history of commits, including their hashes, messages, and details.

Example output:

Here, a1b2c3d is the commit hash you might want to revert.

Step 2: Run the Revert Command


Once you have the commit hash, you can revert it using:

This tells Git to create a new commit that undoes the changes from the specified commit.

Step 3: Handle Any Conflicts

If Git detects conflicts, you’ll see something like this:

You can resolve conflicts manually by opening the file and fixing the code, or you can use:

After resolving conflicts, mark them as resolved:

Step 4: Update Commit Message (Optional)


When reverting, Git opens a text editor (like Vim) to confirm or edit the commit message:

This reverts commit a1b2c3d

You can leave it as is, or edit to make it clearer, for example:

Reverted the login bug fix due to errors in production 

Step 5: Push the Reverted Commit

 Finally, push the changes to your remote repository:

This ensures the reverted commit is visible to your team and becomes part of the shared history.

How To Revert Multiple Commits In Git?

Sometimes, you might want to undo multiple commits in Git without modifying history, particularly when you are working with a team or after pushing changes into a remote branch. Rather than eliminate them, Git generates new “revert” commits that safely reverse the unwanted changes.

Syntax:

  • The caret (^) ensures that the oldest commit is also included in the revert.
  • Git will go through each commit in the range and generate a new commit that reverses it.

Example: Rolling Back a Buggy Feature

Let’s assume that  your commit history looks like this:

You realize the entire search feature introduced multiple issues and want to undo all three commits.

Run:

Result:

  • Git will generate three new commits, each reversing the changes made by the original commits.
  • Your history stays intact, and teammates won’t face conflicts due to rewritten history.

How to Revert a Commit in Git After Push

If you’ve already pushed changes to a remote repository, the safest way to undo is with git revert. Unlike history rewriting commands, revert makes a new commit that undoes the changes, so your project history shows all of your work and is collaboration-friendly.
Here’s a step-by-step guide:

Step 1:  Check the Commit History

First, identify the commit you want to undo by running:

Example output:

Here, let’s say you want to revert commit 3e1f1b2.

Step 2: Revert the Commit

Now, run:

  • Git will generate a new commit that undoes the changes made by 3e1f1b2.
  • An editor (like Vim) may open so you can confirm or edit the default commit message.

Step 3: Push the Revert Commit

Finally, push the new commit to the remote branch:

This ensures the reverted changes are reflected in the shared repository without affecting other collaborators.

Pro Tip: If you want to revert multiple commits after push, you can use a commit range with:

Git revert <oldest_commit_hash>^. . <newest_commit_hash>

Git Revert Vs Git Reset

AspectGit RevertGit Reset
PurposeCreates a new commit that undoes the changes of a previous commit without altering history.Moves the branch pointer to a previous commit, effectively removing later commits.
HistoryPreserves commit history by appending a new inverse commit.Alter commit history, which can cause problems in shared repositories.
SafetySafe to use in public/shared branches because history remains intact.Risky for shared branches as it rewrites history.
Undo StyleNon-destructive: adds a new commit to cancel out changes.Destructive: removes or rewrites commits.
Common Use CaseWhen you want to fix or undo a specific commit while keeping history clear and collaborative.When you want to completely discard commits or reset the branch to a previous state.

Conclusion

In summary, understanding how to revert a commit in Git is an essential skill for keeping your codebase stable and collaboration smooth. Whether you need to undo a single mistake, learn how to revert a commit to master in Git, or handle situations where you must revert a commit in Git after push, using git revert ensures history is preserved while problems are fixed. And in cases where broader rollbacks are needed, knowing how to revert to a previous commit in Git gives you the flexibility to manage your project with confidence. By mastering these techniques, you can maintain a clean, reliable, and professional workflow—whether you’re working solo or with a team.

Advait Upadhyay

Advait Upadhyay (Co-Founder & Managing Director)

Advait Upadhyay is the co-founder of Talentelgia Technologies and brings years of real-world experience to the table. As a tech enthusiast, he’s always exploring the emerging landscape of technology and loves to share his insights through his blog posts. Advait enjoys writing because he wants to help business owners and companies create apps that are easy to use and meet their needs. He’s dedicated to looking for new ways to improve, which keeps his team motivated and helps make sure that clients see them as their go-to partner for custom web and mobile software development. Advait believes strongly in working together as one united team to achieve common goals, a philosophy that has helped build Talentelgia Technologies into the company it is today.
View More About Advait Upadhyay
India

Dibon Building, Ground Floor, Plot No ITC-2, Sector 67 Mohali, Punjab (160062)

Business: +91-814-611-1801
USA

7110 Station House Rd Elkridge MD 21075

Business: +1-240-751-5525
Dubai

DDP, Building A1, IFZA Business Park - Dubai Silicon Oasis - Dubai - UAE

Business: +971 565-096-650
Australia

G01, 8 Merriville Road, Kellyville Ridge NSW 2155, Australia

call-icon