How to use git revert properly [4 Different Ways]

 

What is git revert

Revert is the act of undoing a commit or change in a git environment. To revert or undo in git is facilitated by the git revert function. This command provides a unique way of undoing changes to a specific commit while maintaining the branch history. It simply lets you deal with the specific commit to revert or the changes that might have taken place on it. The revert function is best utilized when dealing with bugs as you won’t have to mess with the entire repository to remove them.

Git revert is just another form of a commit meant to undo an earlier change or a commit that is faulty. Using git revert commit makes it the best alternative to work with when managing wrong commits while maintaining the quality of the code. This is especially when dealing with a collaborated project as you won’t tamper with the already build history on the working repository.

Advertisement

In the tutorial, we will explain the git revert command concept, how it works in a git environment and demonstrate using examples the different git revert methods.

 

Git revert syntax

You can use the following basic syntax to perform a revert operation. To get the complete list of supported options you can follow official git documentation page:

$ git revert [--[no-]edit] [-n] [-m parent-number] [-s] [-S[<keyid>]] <commit>...

Git revert allows for different options to be adapted in its execution as detailed below;

  • commit: this reverts a commit in an active branch. It requires referencing the commit id to be affected.
  • --edit: enables the editing of the commit message before running the commit
  • -m parent number or --mainline parent number facilitates merge revert. Specifying the parent number helps to revert the targeted parent without confusion. That is because when you revert a git merge you will be dealing with two parents commit.
  • --n or --no edit: stops the default revert commit from taking place. Using this option enables reverting of several commits in a row.

 

Git revert workflow

Compared to other functions like git reset and git checkout, git revert command does not make the HEAD point to the last commit. Once a specified commit is undone, git revert creates a new revert commit which is pushed at the top of the branch. Git revert never positions the referenced branch to point to the new revert commit.

How to use git revert properly [4 Different Ways]
git revert workflow
From the diagram, the second last commit from the left has been reverted to create a new revert commit and on top of the tree as pointed by the arrow.

We will illustrate further in the next section on how git revert works using examples.

Advertisement

 

Setting up the lab environment

You will first need to create a working environment that you will use to practice how to use git revert command. For this tutorial, I will create a working repository git-sample and clone it to my local workstation windows 10 pro.

$ git clone https://github.com/Maureen-Mulombi/git-sample.git

Sample Output:

How to use git revert properly [4 Different Ways]

We have a local repository git-sample set up in git version 2.32.0.windows.2 and ready to use for practising different git revert methods as follows:

 

Different git revert methods

Method-1: Perform git revert to the latest commit

To understand how git revert latest commit functions, we will add some files n-file.css and newfile.js  in our local repository bug-fix branch. Next, we will run the git log --oneline command to view our new commits as follows:

$ git log --oneline
467213f (HEAD -> bug-fix) added a n-file.js file
4e37d35 added a newfile.css file
f0df16c (origin/master, origin/HEAD, master) Initial commit

Notice the two files n-file.css  and newfile.js file in our results.

Assuming that committing the newfile.js in the bug-fix branch was wrong, we will revert the changes as below;

$ git revert <commit>

Sample Output:
How to use git revert properly [4 Different Ways]

 

We will run the  git status command to see the deleted file as shown below:

Advertisement
$ git status
On branch bug-fix
Your branch is up to date with 'origin/bug-fix'.
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    newfile.js

Next, we will run the git log command again to see the git revert command changes as follows:

$ git log --oneline
f330e80 (HEAD -> bug-fix) Revert "added a js file"
467213f added a js file
4e37d35 added a css file
f0df16c (origin/master, origin/HEAD, master) Initial commit

The results show that we have a new revert commit f330e80 as well as the initial commit we reverted 467213f. Like we mentioned at the beginning, the git revert command doesn’t alter the active branch history but rather creates a new revert commit as demonstrated here. Also, notice that the HEAD is pointed to the active branch and not the last commit which is the expected event in a git revert operation.

 

Method-2: Perform git revert merge

To perform git revert merge we use the following syntax:

$ git revert merge -m 1 <merge-commit id>

-m 1 implies that we are reverting to the first parent since a merge is between two-parent branches.

To demonstrate how git revert merge -m 1 <commit id> command works, we will create a new branch feature in our current local repo git-sample as shown below;

$ git checkout -b feature
Switched to a new branch 'feature'

Next, we will run a few commits into the feature branch and commit it upstream as illustrated here:
How to use git revert properly [4 Different Ways]

 

Now we will pick a commit from the feature branch to merge it into the master branch. But first, we will have to run the $ git log --oneline command so as to pick the commit we want to use as follows:

$ git log --oneline
e8e4bd3 (HEAD -> feature, origin/feature) testing.css
90b09b0 (origin/master, origin/HEAD, master) Initial commit

Let’s pick commit e8e4bd3 testing.css to merge into the master branch as shown here:

First, we will run the git checkout master command:

$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master' 

Next, we run the git merge command:

$ git merge e8e4bd3
Updating 90b09b0..e8e4bd3
Fast-forward
 fet-test.js | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 testing.css

Now at this stage, we realize that we wrongfully merged commit e8e4bd3-testing.css after pushing the changes upstream and would therefore like to revert the merge.

 

To revert the git merge command changes we will use the git revert –m 1 <commit id> command followed by the git log --oneline command to see the revert changes as illustrated. –m 1 implies that we are reverting to the first parent of the commit which in this case if the feature branch.

$ git revert e8e4bd3
Revert "testing.css"
This reverts commit 152553c8b42453891b4db0dd8a064b393a8367d1.
 On branch master
 Your branch is up to date with 'origin/master'.
 Changes to be committed:
       deleted:    testing.css

Next:

Let’s run git log --oneline command to see the new revert commit in the current branch.

$ git log --oneline
1648442 ((HEAD -> master, feature) Revert "testing.css"
f929666 (feature) testing.css
e8e4bd3 (origin/feature) fet-test file added
90b09b0 Initial commit

From the results, note the new revert commit testing.css at the top which will leave the master branch as it was before the merge.

 

Method-3: git revert multiple commits with single command

It is also possible to revert more than one commit using the git revert <commit> syntax.

First, let’s create a new branch mytest using git checkout -b <branch> command in our local repository git-sample as shown;

$ git checkout -b mytest
Switched to a new branch 'mytest'

Next, we run git log –oneline command to view all the commits in mytest branch as follows;

$ git log --oneline
75f86bd (HEAD -> mytest) s-three file
d404b11 sample-two file
1648442 (master) Revert "testing.css"
f929666 (feature) testing.css
e8e4bd3 (origin/master, origin/feature, origin/HEAD) fet-test file added
90b09b0 Initial commit

We will then proceed to push the changes upstream to keep the work updated remotely as shown below;

How to use git revert properly [4 Different Ways]

 

While at this point, we want to revert the last two commits 75f86bd and d404b11 shown in the git log --oneline command output as they were not meant for this branch.

To revert the two or more commits, we will need to put all their commits id’s in the git revert <commit A commit B> command as follows;

$ git revert 75f8bd d404b11
Revert "sample-two file"
This reverts commit d404b114ab2f799a39cfa329960bb72d2f4018bd.
On branch mytest
Your branch is up to date with 'origin/mytest'.
 Changes to be committed:
       deleted:    sample-two.txt

Revert "s-three file"
This reverts commit d404b114ab2f799a39cfa329960bb72d2f4018bd.
On branch mytest
Your branch is up to date with 'origin/mytest'.
 Changes to be committed:
       deleted:    s-three.txt

Here git creates separate reverts and we can view the reverts using the git log –-oneline command as shown below:

$ git log --oneline
4fba226 (HEAD -> mytest, origin/mytest) Revert "s-three file"
5e9cce8 sample.txt
9523a46 s-three file
53b65fa Revert "sample-two file"
75f86bd sample-two file
d404b11 sample file
1648442 (master) Revert "testing.css"
f929666 (feature) testing.css
e8e4bd3 (origin/master, origin/feature, origin/HEAD) fet-test file added
90b09b0 Initial commit

 

Method-4: git revert between a range of commits

You can use git revert command to revert between a range of commits as illustrated in the example below:

We shall use mytest branch in our active repo git-sample for this experiment. Let’s start by running the git log --oneline command to view all its commits as follows;

$ git log --oneline
462ccee (HEAD -> mytest) ftest-4file
5c28e45 ftest-3 file
ce9c2c1 ftest-2 file
3e46e7c ftest file
4fba226 (origin/mytest) Revert "s-three file"
5e9cce8 sample.txt
9523a46 s-three file
53b65fa Revert "sample-two file"
75f86bd sample-two file
d404b11 sample file
1648442 (master) Revert "testing.css"
f929666 (feature) testing.css
e8e4bd3 (origin/master, origin/feature, origin/HEAD) fet-test file added
90b09b0 Initial commit

To revert between a range of commits we will use the git revert head command as shown below:

Here we shall try to revert the last three commits: 462ccee, 5c28e45 and ce9c2c1

$ git revert HEAD~3..HEAD
Revert "ftest-4file"
This reverts commit 462cceef3d55c3a38a97fc85bfe80594ba7574a6.
On branch mytest
 Your branch is ahead of 'origin/mytest' by 4 commits.
   (use "git push" to publish your local commits)
 Changes to be committed:
       deleted:    ftest-4.js

Revert "ftest-3file"
This reverts commit 462cceef3d55c3a38a97fc85bfe80594ba7574a6.
On branch mytest
 Your branch is ahead of 'origin/mytest' by 3 commits.
   (use "git push" to publish your local commits)
 Changes to be committed:
       deleted:    ftest-3.txt

Revert "ftest-2file"
This reverts commit 462cceef3d55c3a38a97fc85bfe80594ba7574a6.
On branch mytest
 Your branch is ahead of 'origin/mytest' by 1 commits.
   (use "git push" to publish your local commits)
 Changes to be committed:
       deleted:    ftest-2.css

Note that throughout the operation git revert head~, git gives reverts separately ranging from the latest commit backwards. Also, git will also prompt you to commit any pending commits in every single commit revert stage before moving to the next.

 

Summary

We have covered the following topics:

  • What is git revert
  • Git revert syntax
  • Git revert methods

 

Further reading

git revert-command

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment