Please note, this is a STATIC archive of website www.simplilearn.com from 27 Mar 2023, cach3.com does not collect or store any user information, there is no "phishing" involved.

We all make mistakes; that’s why pencils have erasers! And while the unexpected can throw a wrench into our best designs and plans, things can get incredibly messy in the field of software development and coding. One careless mistake or an unintended patch result can throw an entire project into chaos.

Fortunately, Git has many options to safely undo changes without ruining the whole process. Today we're looking at the Git revert command and the entire process behind Git revert commit, alternately referred to as Git revert to commit. This article defines Git revert, how to revert Git commit, how Git revert changes work, and what we mean by Git revert last commit.

We will also explain the Git revert branch, Git revert file, Git revert merge, and Git revert push.

Become a Skilled Web Developer in Just 9 Months!

Caltech PGP Full Stack DevelopmentExplore Program
Become a Skilled Web Developer in Just 9 Months!

What Is Git Revert?

Before we answer the question "How do you revert Git commit?" we need to explain the command. The Git revert commit command is an "undo" operation that provides users with a careful, less intrusive way to undo changes.

Git revert is a safe, forward-moving alternative to Git reset when faced with the possibility of losing work. Git revert removes all the changes that a single commit made to the source code repository. For instance, if a commit added a file called wombat.html to the repository, the Git revert can remove that file. Or if a previous commit added a line of code to a Python file, a Git revert done on that previous commit will remove the offending line.

How Does Git Revert Work?

Rather than removing the commit in question from the project history, the Git revert figures out how to invert (or reverse) the changes created by the commit, then appends a new commit containing the new inversed content. This process stops Git from losing history, which is vital for the revision history’s integrity and promoting smooth, reliable collaboration.

The ability to Git revert a previous commit comes in handy when you’re tracking down a bug and discover that it was introduced via a single commit. Rather than going in manually, fixing the bug, and committing a new snapshot, Git revert will automatically do all the work for you.

Unlike other "undo" commands like Git checkout and Git reset, which move the branch and HEAD ref pointers to a specific commit, Git revert inverses the changes in a particular commit, creating a new "revert commit." Then, it updates the ref pointers to point at this new revert commit and makes it the branch tip.

Bear in mind, Git revert doesn’t undo a commit in a project history; it reverses the changes done in the offending commit and appends a new commit with the opposite effect.

Resetting vs. Reverting

There are two notable differences between Git revert and Git reset. First, Git revert can focus on one particular commit at a chosen point in history, while Git reset only works backward from the user's current commit. So, whereas a Git revert can target one old commit, a Git reset is more involved. The programmer must remove every commit that occurred after the offending commit, remove the target commit, and put all subsequent commits back in place. This process is time-consuming and inelegant. Since Git reset and Git revert have a shared affinity, we will explain a little more about Git reset at the end of this article.

How Do You Revert Git Commit?

To perform a Git revert commit, you must have the ID number of the commit in question.

The Steps to Follow

Here are the steps you must follow to perform a Git revert on a commit, undoing the unwanted changes:

  • Use the Git log or reflog command to find the ID of the commit you want to revert
  • Enter the Git revert command (see below), including the commit ID you want to work on
  • Provide an informative Git commit message to explain why you needed to perform the Git revert

The Git Commit Message

Git commit messages explain why a developer had to perform a Git revert commit to the team. Therefore, it’s essential to follow the seven accepted rules of standardized Git commit message writing:

  1. Obey the 50-character limit for the subject line
  2. Capitalize just the first letter in the subject line.
  3. Do not place a period at the end of the subject line.
  4. Place a blank line between the subject line and the body of the message.
  5. Limit the message body width to 72 characters.
  6. Write in the imperative.
  7. Explain what was done and why it was done, but not how it was done.

Git Revert Command

The Git revert command syntax is straightforward. Here’s what the syntax would look like if you wanted to revert a Git commit called 31416p54:

git revert 31416p54

Create and Showcase Your Portfolio from Scratch!

Caltech PGP Full Stack DevelopmentExplore Program
Create and Showcase Your Portfolio from Scratch!

Git Revert Commit: An Example

Here’s an example of how to revert Git commit using syntax resources from TechTarget.

We begin by creating a clean repository with the git init command.

git@commit /c/revert example/

$ git init

Initialized empty Git repo in C:/git revert example

Now, we add five files to the repository (or “repo”). Whenever we create a new file, we add it to our Git index and create a new commit that contains a meaningful message.

git@commit /c/revert example/

$ touch alpha.html

$ git add . && git commit -m "1st git commit: 1 file"

$ touch beta.html

$ git add . && git commit -m "2nd git commit: 2 files"

$ touch charlie.html

$ git add . && git commit -m "3rd git commit: 3 files"

$ touch delta.html

$ git add . && git commit -m "4th git commit: 4 files"

$ touch edison.html

$ git add . && git commit -m "5th git commit: 5 files"

After the initial command batch, we can do a directory listing and see five files residing in the current folder.

git@commit /c/revert example/

$ ls

alpha.html  beta.html  charlie.html delta.html  edison.html

And the git reflog command reveals the history of the current commits.

git@commit /c/revert example/

$ git reflog

(HEAD -> master)

d846aa8 HEAD@{0}: commit: 5th git commit: 5 files

0c59891 HEAD@{1}: commit: 4th git commit: 4 files

4945db2 HEAD@{2}: commit: 3rd git commit: 3 files

defc4eb HEAD@{3}: commit: 2nd git commit: 2 files

2938ee3 HEAD@{4}: commit: 1st git commit: 1 file

So, we decide that the charlie.html file needs to go. That’s the third commit, with the ID of 4945db2. We enter the following Git revert command:

git@commit /c/revert example/

$ git revert 4945db2

And presto! The charlie.html is gone, as another directory listing will verify:

git@commit /c/revert example/

$ ls

alpha.html  beta.html  delta.html  edison.html

Basics to Advanced - Learn It All!

Caltech PGP Full Stack DevelopmentExplore Program
Basics to Advanced - Learn It All!

How Do You Revert Commit in GitKraken?

If you use the Git tool known as GitKraken, you need to right-click on a commit from the central graph and choose revert commit from the context menu. You'll be asked if you want to commit the changes now. You can either decide to save your reverted commit or choose no and change the Git commit message or make additional changes to the code.

Revert a Commit in GitLens

If you use GitLens, just open your repo in VS Code, then right-click on the chosen commit found in the Commits view located in the sidebar, getting the Revert Commit option.

Revert a Commit in GitKraken CLI

If you have the free GitKraken CLI installed, use the git revert command as follows:

git revert <commit>

The <commit> value is anything that follows the criteria of a Gitrevision, which Git defines. Gitrevisions are typically a part of the commit sha, so you’ll have to include enough of the sha so that GitKraken CLI can identify the commit in question as unique in that particular Git repository, like so:

git revert 45111a

Check out GitKrakenCLI for more options and information.

Get All Your Questions Answered Here!

Caltech PGP Full Stack DevelopmentExplore Program
Get All Your Questions Answered Here!

Explaining the Git Reset

Like Git revert, Git reset is a tool used to undo changes. Git reset has three primary invocation forms, corresponding with three command-line arguments: --soft, --mixed and –hard. The arguments, in turn, correspond to Git’s three internal state management mechanisms:

  • Commit Tree (HEAD)
  • Staging Index
  • Working Directory

Git reset moves the repository back to an earlier commit, deleting any changes made after the commit in question.

Programmers can perform a Git reset revert by typing:

git reset <file>

This action removes the named file from the staging area, and the working directory remains unchanged. However, if you want to reset the staging area so it matches the most recent commit while leaving the directory untouched, you type:

git reset

But be careful! Git reset is one of the few Git commands that risk the user losing their work! It can undo local changes in both the Working Directory and the Staging Index.

Get access and complete hands-on experience on a plethora of software development skills in our unique Job Guarantee bootcamp. Get job-ready with HackerEarth and HIRIST by enrolling in our comprehensive Post Graduate Program In Full Stack Web Development program today!

How Would You Like to Become a Full-Stack Developer?

There is a consistently strong demand for full-stack Java developers today. If you are looking to either make a career change or upskill your developer knowledge, Simplilearn has the tools you need to make it a reality.

Learn from the Best in the Industry!

Caltech PGP Full Stack DevelopmentExplore Program
Learn from the Best in the Industry!

The Post Graduate Program In Full Stack Web Development is designed to give you an understanding of front-end, middleware, and back-end Java web developer technologies. The course leverages hands-on training and phase-end and capstone projects to train you to build an end-to-end application, test and deploy code, store data using MongoDB, and much more.

Ziprecruiter reports that a Java full-stack developer in the United States earns an average of $119,892 per year. Additionally, Glassdoor shows that a Java full-stack developer in India makes an annual average of ₹554,636.

If you’re ready to take your developer career and skill set to the next level, visit Simplilearn and take advantage of their excellent training resources. Software and app development companies are looking to hire knowledgeable Java full-stack developers and are prepared to make it worth their while. So could that next developer be you?

About the Author

John TerraJohn Terra

John Terra lives in Nashua, New Hampshire and has been writing freelance since 1986. Besides his volume of work in the gaming industry, he has written articles for Inc.Magazine and Computer Shopper, as well as software reviews for ZDNet. More recently, he has done extensive work as a professional blogger. His hobbies include running, gaming, and consuming craft beers. His refrigerator is Wi-Fi compliant.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.