Table of contents
1.
Introduction
2.
What Is Git Revert?
2.1.
A git revert commit example
3.
How Does Git Revert Work?
4.
Installation
5.
Real-World Scenario
6.
Edge Cases
7.
Advantages and Disadvantages
7.1.
Advantages
7.2.
Disadvantages
8.
Difference between Resetting and Reverting
9.
Future
10.
Frequently Asked Questions
10.1.
How do I revert to initial commit in git?
10.2.
How to remove previous commit in git?
10.3.
How do I edit or revert a commit message in git?
11.
Conclusion
Last Updated: Aug 13, 2025
Easy

How to Revert a Commit in Git? (With Example)

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In the dynamic world of software development, errors are inevitable. The real prowess lies in how efficiently and swiftly one can rectify those errors to ensure seamless project progression. This is where the git revert command comes into play within the vast and robust ecosystem of Git, a version control system par excellence. The git revert command is your gateway to undoing changes in your project while keeping the history intact. 

revert commit git

This article is a comprehensive guide to understanding and mastering the git revert command, adorned with practical examples, edge cases, advantages, and a glimpse into what the future holds for this indispensable Git feature.

What Is Git Revert?

git revert is a Git command used to reverse changes made in a specific commit. It creates a new commit that undoes the changes of the specified commit.

A git revert commit example

# To revert the last commit

git revert HEAD

 

# To revert a specific commit (e.g., with hash abc123)

git revert abc123

 

This will create a new commit that undoes the changes introduced by the specified commit.

How Does Git Revert Work?

To revert a Git commit, you can use the git revert command. This command creates a new commit that undoes the changes made in a specific commit. Here's how it works:

1. Identify the Commit to Revert: Find the commit hash or reference of the commit you want to revert. You can use git log to see the commit history.

2. Run the Revert Command: Use the git revert command followed by the commit hash or reference.

git revert <commit-hash>

 

If you want to revert the last commit, you can use HEAD as the reference.

git revert HEAD

 

3. Resolve Conflicts (if any): Git may prompt you to resolve conflicts if the changes you are trying to revert conflict with other changes. Resolve conflicts manually and continue.

4. Complete the Revert: After resolving conflicts, commit the changes to complete the revert. 

git commit -m "Revert changes from commit <commit-hash>"

 

5. Push Changes (if needed): If you are working in a shared repository, push the changes to the remote repository.

git push origin <branch-name>

Installation

Embarking on the journey of mastering Git begins with its installation. Here’s how you can install Git on a Linux machine:

sudo apt-get install git

Now that Git is installed, let's dive into a real-world scenario to understand the git revert command better.

Real-World Scenario

Imagine you are developing a simple web application comprising three core files: index.html, style.css, and script.js. Your project directory looks like this:

/project-directory
    /index.html
    /style.css
    /script.js

You have recently added some new CSS styles and JavaScript functions. However, after reviewing, you realize that the new JavaScript function is causing bugs. You decide to revert the changes made in the last commit to the script.js file.

Firstly, ensure that you are in the correct directory and check the commit history:

cd project-directory
git log --oneline

Suppose the commit hash for the changes you want to revert is abc123. You would execute the following command to revert the changes:

git revert abc123

Git will prompt you to enter a commit message for this reversion. Save and exit the text editor to complete the reversion process.

Edge Cases

Reverting a merge commit can be a tricky affair, as it requires specifying the parent number if conflicts arise. Here's how you can do it:

git revert -m 1 <merge-commit-hash>

Advantages and Disadvantages

Let's explore the advantages and disadvantages of revert commit git: 

Advantages

  • Error Correction: Swift reversion to correct errors and maintain project stability.
     
  • History Preservation: Git revert keeps the commit history intact, promoting better traceability.

Disadvantages

Conflict Resolution: Reverting merge commits may lead to conflicts, requiring manual resolution.

Difference between Resetting and Reverting

Resetting Reverting
Changes HEAD and branch pointer. Creates a new commit to undo changes.
Hard reset discards all changes. Creates a new commit with reversed changes.
Useful for discarding local changes. Safer, preserves commit history.
Can be force-pushed to remote. Safe for shared branches.

Future

As projects evolve and become more complex, the git revert command will continue to be a staple in a developer's toolkit, offering a reliable solution for correcting mistakes while preserving project history.

Frequently Asked Questions

How do I revert to initial commit in git?

To revert to the initial commit in Git, use the command: git reset --hard <initial-commit-hash>

How to remove previous commit in git?

To remove the previous commit, use: git reset --hard HEAD^

How do I edit or revert a commit message in git?

To edit or revert a commit message, use: git commit --amend -m "New commit message"

Conclusion

The git revert command is an indispensable tool in the Git ecosystem, offering a reliable mechanism to undo changes and correct mistakes in a project. Its non-destructive nature, coupled with its ability to preserve project history, makes it a preferred choice for developers. As we stride into the future, the git revert command will continue to hold its significance, aiding developers in maintaining the integrity and stability of their projects amidst the ever-evolving landscape of software development.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass