Skip to content

How to Revert a Git Commit: A Simple Example

revert git commit


Git, a popular version control system in software development, enables developers to track changes, collaborate effectively, and keep a history of their codebase. The ability to rollback commits is a critical feature of Git. Knowing how to rollback a Git commit is a critical skill for any developer, whether you made a mistake or decided to take your codebase in a different route. In this post, we’ll look at how to use git revert to undo a Git commit, as well as the distinctions between git revert and other frequent commands like git reset.

Why Revert a Git Commit?

Before we go into the specifics, let’s first establish why reversing a Git commit is important. Git is used by developers for a variety of purposes, including collaboration, keeping track of changes, and guaranteeing code stability. However, mistakes can occur. You may have made a modification that broke your code, or you may have determined that the path you picked with your project was not the best one. In such instances, you should be able to undo a commit while keeping the commit history.

In Git, reverting a commit is the safest approach to undo changes while retaining a record of what you did. It generates a new commit that undoes the changes introduced by the commit you want to revert, allowing you to have a clean and consistent history.

Using git revert to Undo a Commit

The git revert command is intended to undo commits in a safe and non-destructive manner. It creates a new commit that reverses the changes made in the previous commit. This is especially beneficial when you wish to maintain a clean and linear commit history, as opposed to the more aggressive git reset command, which may alter history.

Here’s how to use git revert to undo a change step by step:

Step 1: Identify the Commit to Revert

Before you can revert a commit, you need to know the commit’s unique identifier (SHA-1 hash) that you want to revert. You can find this identifier by running the git log command, which displays a list of commits along with their SHA-1 hashes.

git log

Once you have identified the commit you want to revert, copy its SHA-1 hash.

Step 2: Run git revert

Now that you have the commit’s SHA-1 hash, you can use the git revert command to create a new commit that undoes the changes from the specified commit.

git revert <commit-SHA-1>

Replace <commit-SHA-1> with the SHA-1 hash of the commit to be reverted. As an example:

git revert c1a2b3d4

Step 3: Write a Commit Message

Git will open your normal text editor and prompt you to write a commit message for the new revert commit. Git will automatically generate a commit statement that explains the revert action. You can keep the default message or change it to provide extra explanation if necessary.

Step 4: Save and Close the Text Editor

Save and exit the text editor once you’ve finished writing your commit message. Git will generate a new commit that reverses the modifications made in the previous commit, and the commit history will be updated accordingly.

Comparing git revert to git reset

While git revert is a safe and non-destructive technique to undo a commit, another Git tool that may be used for the same reason is git reset. However, there are important distinctions between the two:

Preservation of Commit History: git reverse generates a fresh commit that undoes modifications while maintaining the commit history. Git reset, on the other hand, can be damaging because it removes commits from the branch, possibly changing history. This makes git revert a more secure alternative, particularly when working in a shared repository where changing history can lead to conflicts.

Collaborative Work: When working in a group, using git revert is a more collaborative technique. It enables you to rollback changes without harming other team members who may have already pulled the commit you need to undo.

Use Cases: git reset is more suitable for situations where you want to completely remove one or more commits and start over from a specific point in your commit history. It’s a powerful but potentially dangerous command that should be used with caution.

To summarise, git revert is the preferred method for undoing a commit while retaining a clean and collaborative commit history. git reset, on the other hand, is a more advanced and potentially harmful command that should be used with caution and knowledge of the repercussions.

Frequently Asked Questions (FAQs)

What is the purpose of reverting a Git commit?

Reverting a Git commit allows you to undo changes made by a specific commit while keeping the commit history intact. It enables developers to rectify mistakes, remove undesired changes, or change the direction of their codebase without affecting the chronological order of commits.

How does git revert differ from git reset?

git revert and git reset are both used to undo commits, but they have significant differences:

git revert creates a new commit that undoes the changes from a previous commit while preserving commit history. It is safe and non-destructive, making it suitable for collaborative work.

git reset can be destructive, as it removes commits from the branch, potentially rewriting history. It’s more suitable when you want to start over from a specific point in your commit history, but it should be used with caution.

Is it possible to revert multiple commits at once?

Yes, you can revert multiple commits at once by specifying a range of commits in the git revert command. For example:

git revert <commit-SHA-1-1>..<commit-SHA-1-2>

This command will create a new commit that undoes the changes introduced by all the commits in the specified range.

Can I revert a commit from a different branch?

Yes, you can revert a commit from a different branch by specifying the commit’s SHA-1 hash when using git revert. However, it’s essential to be aware that this action will create a new commit in your current branch, undoing changes from the commit in the other branch.

Related Articles

Difference Between Git Stash Pop And Git Stash Apply

What Is Git Stash And Why Do You Need It?

Git Delete Remote Branch – How To Remove A Remote Branch In Git

MongoDB Interview Questions And Anwers

Understand The Background Of Free AI Tool Now

The Requested Url /Phpmyadmin/ Was Not Found On This Server


Learning how to undo a Git commit is a must for any developer who uses Git as their version control system. The git reverse command is a useful tool in your Git toolbox because it allows you to undo a commit while retaining your commit history. Remember that git reset is a more powerful but possibly dangerous option, so use it with caution and always discuss with your team before making large changes to your Git history.

By understanding these Git commands, you’ll be better able to handle the complexity of version control, work successfully with your team, and maintain your codebase clean and organised, resulting in a smoother and more productive development process. So, go ahead and start using git revert to undo those undesired commits and keep your Git history in pristine condition.