How To Revert A Commit With Git Revert?
Last Updated :
26 Feb, 2024
Git is a distributed version control system that tracks changes in any set of computer files, usually used for coordinating work among programmers who are collaboratively developing source code during software development. It is free, open-source, and used by almost 95% of developers across the world. There are many popular offerings of Git repository services, including GitHub, Bitbucket, etc.
Git Commit
A commit in Git represents a snapshot of your repository at a given point in time. It includes changes made to files and a commit message describing the changes.
Syntax of git commit command
git commit -m "commit message"
To know more about git commit, check out this article.
Git Revert
It might happen that we make some changes and then make a commit but later we discover a bug and we want to undo those changes and restore the project state to the previous commit so it does not create problems for others and people can work smoothly on the project. The git revert command helps us to do that.
Syntax of git revert command
git revert <commit-hash>
Points To Remember
- When we do a git revert it does not delete the reverted commit from the commit history instead it stages the changes for another commit with the reverted changes.
- When you revert a commit c then all the commits made after c will also get reverted.
What is the difference between git revert and git reset?
Git Revert
| Git Reset
|
---|
Revert changes by creating new commits.
| Reset the current HEAD to a specified state.
|
Creates a new commit that undoes the specified commit.
| Moves HEAD and branch pointers to the specified commit.
|
Preserves commit history by adding revert commits.
| Rewrite commit history by removing commits.
|
Preferred for reverting changes on shared branches.
| Should not be used for shared branches due to history rewriting.
|
Syntax: git revert [options] <commit-hash>
| git reset [options] <commit>
|
Steps To Revert A Commit
Step 1: In git, every commit is uniquely identifiable by its commit hash. To revert a commit we must know its hash.
To find the hash of a commit you can use the git log command to get a list of all the commits made along with their commit message, hash, and other details etc.
git log
Step 2: Now we can revert the commit by using the git revert command followed by the commit-hash.
git revert <commit-hash>
Step 3: If there are any conflicts during the revert then you can resolve them by using tools like git mergetool or you can edit the conflicted files manually.
Step 4: After that, the vim edit is opened where you can edit the commit message for the reverted message.
Step 5: Now, a new commit is added and then we can use the git push command to push the reverted changes to the branch.
Flags (options that can be used with git revert)
1. no-commit : Since the git revert command adds a new commit by default. This flag to applies the revert to your working directory and staging area but doesn't create a new commit. It allows you to make further modifications or additions before committing the reversion.
git revert --no-commit <commit-hash>
2. -m parent-number : If you're reverting a merge commit, this flag allows you to specify which parent's changes you want to revert. You provide the parent number (starting from 1) to indicate which parent's changes should be reverted.
git revert -m 1 <merge-commit-hash>
3. --no-edit : By default when we run the git revert command, a vim editor is shown where we can edit the commit message for the reverted message. However, if we do not want to change the commit message and use the default message then we can use this flag.
git revert --no-edit <commit-hash>
Example:
Let's say we have a git repository called 'GFG'. It is currently empty. Let's create a new text file named 'intro.txt' and then make a git commit.

Lets make add some content to 'gfg.txt' and then make another git commit.

Now, suppose we want to undo the previous commit. We can start by using the git log command to see the list of all commits made.

Now, we can see the commit hash of the commit hash of the commit we want to revert. So, lets revert it by using the git revert command.

After running this command, you will see a vim editor on the terminal. Here's a quick article to know more about vim. The vim editor will help you to edit the commit message of the reverted commit. You can edit it and then press Ctrl +C on Windows (Cmd + C on Mac) and then follow the instructions to exit it.

Now, if we do git commit, we can see a commit is added with the mesage we gave in the vim editor.

Finally, push the reverted changes to the remote by using the git push command.

Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read
Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc
15+ min read