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
How to Revert a Git Commit?
In software development, mistakes happen, and there will be times when you need to undo changes in your Git repository. Reverting a Git commit is a common task that can be handled in several ways, depending on your needs and the state of your project. This article will guide you through the differen
4 min read
How to Revert a Pushed Merge Commit in Git?
In Git, merge commits are created when integrating changes from one branch into another. Sometimes, you may find yourself in a situation where you need to revert a merge commit that has already been pushed to a remote repository. Reverting a merge commit requires careful consideration to maintain th
3 min read
How to Revert to Last Commit?
Reverting to the last commit in Git is an important skill for developers who need to undo changes and return their project to a previous state. This article will guide you through various approaches to revert to the last commit, detailing each step to ensure you can effectively manage your Git repos
2 min read
How to Revert Multiple Git Commits?
Reverting multiple Git commits is a common task when you need to undo a series of changes made to your codebase. This process can be important for fixing mistakes, removing faulty code, or simply rolling back to a previous stable state. Git provides several methods to revert multiple commits, each w
3 min read
How To Revert The Last Merge Commit in Git?
Managing a Git repository often involves merging branches. Sometimes, after completing a merge, you might realize that the merge was a mistake or that it introduced issues. Fortunately, Git provides a simple way to revert the last merge commit. In this article, we will walk through the steps to safe
4 min read
How to Restore a Deleted Branch or Commit with Git?
Deleting a branch or a commit in Git might seem like a disaster, especially if it was done unintentionally. However, Git provides robust tools to help you recover deleted branches or commits. Hereâs a detailed guide on how to restore a deleted branch or commit in Git. Table of Content Understanding
3 min read
How to Tag An Older Commit in Git?
Git tags are a useful way to mark specific points in a repositoryâs history as being important. Typically, tags are used to mark release points (e.g., v1.0, v2.0). While tagging the latest commit is straightforward, there are times when you may need to tag an older commit. This could be because a si
3 min read
How to Back Previous Commit in Git ?
Git is a powerful version control system that allows developers to track changes in their codebase, collaborate with others, and maintain a history of their project. Sometimes, you may find yourself in a situation where you need to revert to a previous commit due to a bug or mistake in the code. In
5 min read
How To Reset Remote Repository to a Certain Commit in Git?
Resetting a remote repository to a specific commit in Git can be an important task, especially when you need to revert changes or roll back to a stable state. This article will guide you on how To Reset Remote Repository to a Certain Commit in Git. Table of Content Approach 1: Using `git reset` and
2 min read
Reverting A File To Previous Commit
In software development, you may encounter situations where you need to revert a file to its state from a previous commit. This could be due to a bug introduced in recent changes, a need to undo experimental modifications, or simply to restore a stable version. Git provides several methods to revert
4 min read