Open In App

How To Revert A Commit With Git Revert?

Last Updated : 26 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

first_commit-min

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

second_commit-min

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.

git-log-min

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.
git-revert

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.

vim

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

revert-commit-added

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

push-revert-commit

Reverting A Commit With Git Revert – FAQ’s

Does git revert delete the original commit?

No, git revert does not delete the original commit. Instead, it creates a new commit that undoes the changes introduced by the original commit.

Can I revert multiple commits at once?

Yes, you can revert multiple commits by specifying their commit hashes in the git revert command separated by spaces.

What if I revert a commit by mistake?

If you revert a commit by mistake, you can use git reflog to find the commit hash of the revert commit and then revert the revert commit to restore the original changes.

Does git revert work for merge commits?

Yes, git revert can be used to revert merge commits. It will create a new commit that undoes the changes introduced by the merge commit.

Can I revert a revert commit?

Yes, you can revert a revert commit using the same process described above. This will effectively reapply the changes reverted by the original revert commit.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads