Sergey Nivens - Fotolia

Problem solve Get help with specific problems with your technologies, process and projects.

How to git revert a commit: A simple undo changes example

The best way to undo previous changes to the source code repository is to learn how to git revert a commit.

Two of my favorite source code control commands for working with previous commits are git cherry-pick and git revert. The git cherry-pick command is great when you need to pull in a bug fix from another active branch. And the ability to git revert a commit is great way to back out of a bad contribution to the code base. But as useful as these commands are, I rarely see devs practice them on active projects. Team leads often go to exorbitant lengths to undo a problem in their source code repository. Really, they could just git revert a troublesome commit instead. Here, we'll provide a simple git revert example to show how the command works and how you can use it to revert a specific commit.

A git revert example

In the name of simplicity, this git revert example will start off with a completely clean repository. That means issuing a quick git init command.

/c/ git revert changes example
$ git init
Initialized empty Git repository in C:/ git revert changes example/.git/

Now that we've initialized the Git repository, create five HTML files, and perform a git commit with each one. To keep track of the commit history, each commit will include the commit number along with the count of the number of files in the working tree. Now, we can git revert a previous commit and examine the state in which it leaves our development environments.

/c/ git revert changes example
$ echo 'alice' > alpha.html
$ git add . && git commit -m "1st git commit: 1 file"

$ echo 'becky' > beta.html
$ git add . && git commit -m "2nd git commit: 2 files"

$ echo 'callie' > charlie.html
$ git add . && git commit -m "3rd git commit: 3 files"

$ echo 'diana' > delta.html
$ git add . && git commit -m "4th git commit: 4 files"

$ echo 'ellen' > edison.html
$ git add . && git commit -m "5th git commit: 5 files"

With this git revert example, you'll learn how to undo a previous source code repository update.

To recap this git revert example, we have created five HTML files and executed a commit for each one. We can look at the history if we invoke the git reflog command:

/c/ git revert changes 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

What happens when we git revert a commit?

At this point, we have five files in the working directory. So, what exactly happens when we git revert a commit? If we performed a git revert on the third git commit -- the one with charlie.html -- in what type of state would git leave our development environment? When developers git revert a commit for the first time, they usually guess the command will do one of three things:

  1. The git revert will leave two files on the file system -- alpha.html and beta.html -- and will roll back to the state prior to the third commit.
  2. The git revert will leave three files on the file system -- alpha.html, beta.html and charlie.html -- and will roll back to the state at which the third commit occurred.
  3. The git revert will leave us with four files and remove only charlie.html, the only file associated exclusively with the third commit.
git revert a commit
Here's the command you'll use to git revert a commit.

So, which is it? We can figure it out if we issue the git revert on the third commit.

/c/ git revert changes example
$ git revert 4945db2

A simple directory listing shows us that four files remain, with the file named charlie.html removed.

/c/ git revert changes example
$ ls
alpha.html  beta.html  delta.html  edison.html

How to git revert a previous commit

As you can see from this git revert example, when you git revert a previous commit, the command only removes the changes associated with that previous commit. Prior changes and those made after that previous commit remain. When you git revert a previous commit, the only things plucked out of your development environment are the changes explicitly associated with the reverted commit. In this case, the command only removes the third commit -- charlie.html -- we added to the source code repository. Again, only that third commit falls victim to the git revert command, as it removes charlie.html from the working tree.

And that's it. That's what happens when you git revert a commit. It's a great way to remove a bug that may have been introduced into the version control system at some point in the past or back out of a feature enhancement that wasn't well-received by the client. It's definitely a feature of Git that can save the DevOps team a lot of time when they need to roll back a small, historic change without wiping out all of the good changes and updates they have subsequently committed.

Next Steps

Mastered Git? The next step in your DevOps journey is mastering Jenkins CI. Here are some great Jenkins tutorials that will take you from beginner to expert.

Step 1 -- How to download, configure and install Jenkins tutorial

Step 2 -- Create your first Jenkins freestyle build job example

Step 3 -- Pull from the Jenkins environment variables list in your shell scripts

Step 4 -- Fix common Jenkins plugin installation errors

Step 5 -- Add String and Boolean Jenkins parameters to your builds

Step 6 -- Start pulling from GitHub with the Jenkins Git plugin

Step 7 -- What happens when you git reset hard and push?

Step 8 -- Get a handle on the  Jenkins vs. Maven debate

Step 9 -- Tackle these DevOps interview questions and answers

Step 10 -- Try out these sample Jenkins interview questions

This was last published in June 2018

Dig Deeper on Java DevOps

Join the conversation

1 comment

Send me notifications when other members comment.

Please create a username to comment.

In what type of situation would you use the git command to revert a previous commit?