The “git revert” command performs an amazingly useful operation within the Git environment. At times, you wish to undo the changes that you have once committed without actually removing those changes from the “git log” so that you can always redo them in the future whenever you want. The “git revert” command exactly allows you to do so. It means that you can easily nullify or cancel the effect of any previous commit without actually removing it from your Git history. Therefore, this article is intended to teach you the method of reverting to a previous commit in Git in Ubuntu 20.04.

Method of Reverting to a Previous Commit in Git in Ubuntu 20.04:

For explaining to you the method of reverting to a previous commit in Git in Ubuntu 20.04, we have designed an example scenario. In this scenario, we will first modify an already existing file named abc.txt. Also, we have two other files in our test project repository named Stash1.txt and Stash2.txt that we had not committed previously. So, we will be committing all of these changes at once. Then we will try to revert to a previous state i.e. a state in which neither the files Stash1.txt and Stash2.txt existed nor the file abc.txt was modified. To further elaborate on this scenario, we would like to walk you through the following steps:

Step # 1: Switch to your Git Project Directory:

First, we will go to the directory where our Git project repository resides in the manner shown below:

cd /home/kbuzdar/simplegit-progit

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/word-image-294.png" data-lazy- height="28" src="data:image/svg xml,” width=”580″>

Once this command will execute, it will change the default path of your terminal as shown in the following image:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/word-image-295.png" data-lazy- height="52" src="data:image/svg xml,” width=”588″>

Step # 2: List down the Contents of your Git Project Directory:

Now we will list down the contents of our test project repository to see which files are already there. The contents of our Git project repository can be listed using the command stated below:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/word-image-296.png" data-lazy- height="28" src="data:image/svg xml,” width=”449″>

The contents of our Git project repository are shown in the following image:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/word-image-297.png" data-lazy- height="64" src="data:image/svg xml,” width=”594″>

Step # 3: Open and Modify any File within your Git Project Directory:

We have picked the file abc.txt from our Git project repository for modification. Now we will open this file with the nano editor in the manner shown below:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/word-image-299.png" data-lazy- height="29" src="data:image/svg xml,” width=”583″>

This file has some random text written in it as shown in the following image:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/word-image-301.png" data-lazy- height="577" src="data:image/svg xml,” width=”732″>

We will modify this text by adding a “not” in it as shown in the image below. After making this modification, we will simply exit from the nano editor while saving our file abc.txt.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/word-image-303.png" data-lazy- height="83" src="data:image/svg xml,” width=”726″>

Step # 4: Reinitialize your Git Project Repository:

After making all the desired changes, we need to reinitialize our Git project repository with the help of the following command:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/word-image-306.png" data-lazy- height="30" src="data:image/svg xml,” width=”507″>

After executing this command, Git will reinitialize while displaying the message shown in the image below on your terminal:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/word-image-307.png" data-lazy- height="68" src="data:image/svg xml,” width=”703″>

Step # 5: Add the Changes to your Git Project Repository:

Now we need to add the changes to our Git project repository by executing the following command:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/word-image-309.png" data-lazy- height="29" src="data:image/svg xml,” width=”514″>

The successful execution of this command will not display anything on our Ubuntu 20.04 terminal.

Step # 6: Commit the Newly Made Changes in your Git Project Repository:

After adding the changes to our Git project repository, we will commit these changes with the following command:

git commit –m “Message to be Displayed”

Here, you can change “Message to be Displayed” with the actual message that you want to be displayed while this command executes.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/word-image-311.png" data-lazy- height="48" src="data:image/svg xml,” width=”712″>

When this command will execute, you will notice that our two files that were uncommitted previously i.e. Stash1.txt and Stash2.txt will be committed.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/word-image-314.png" data-lazy- height="134" src="data:image/svg xml,” width=”715″>

Step # 7: Check Git Commit History:

Now we will check the Git commit history to see whether our last commit has been logged or not. The Git commit history can be checked with the following command:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/word-image-315.png" data-lazy- height="23" src="data:image/svg xml,” width=”641″>

You can easily see from the Git commit history shown in the image below that the Head is pointing to our last commit i.e. the transaction in which we committed the files Stash1.txt, Stash2.txt, and abc.txt (after modification). Also, we will note the transaction ID of this commit from the commit history so that we can revert it in the next step while making use of this ID.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/word-image-316.png" data-lazy- height="223" src="data:image/svg xml,” width=”719″>

Step # 8: Perform the “git revert” Operation:

The first five characters of the transaction ID of our last transaction are 220ac. These characters will be used for referring to this commit. Now we will revert this commit with the help of the following command:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/word-image-317.png" data-lazy- height="26" src="data:image/svg xml,” width=”579″>

When this command will execute, it will display a message within nano editor that your transaction with the transaction ID 220ac will be reverted as shown in the image below:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/word-image-318.png" data-lazy- height="532" src="data:image/svg xml,” width=”726″>

You need to press Ctrl X after seeing this message so that you can see the exact status on your terminal as shown in the following image. This status will tell us that the changes made earlier i.e. the modification of abc.txt and the addition of Stash1.txt and Stash2.txt have been reverted. It also means that now our file abc.txt will be back in the previous state i.e. it will represent its original content without modification. Moreover, the files Stash1.txt and Stash2.txt will have been deleted.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/word-image-319.png" data-lazy- height="162" src="data:image/svg xml,” width=”593″>

Step # 9: Check the Git Commit History once Again:

Now we will check our Git commit history once again to see the current situation. If the revert operation has been performed successfully, then the Head of our project will be pointing to this revert transaction as highlighted in the image shown below:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/word-image-320.png" data-lazy- height="254" src="data:image/svg xml,” width=”721″>

Step # 10: Verify if the “git revert” Command has worked successfully or not:

Although, we have already seen that our project Head is currently pointing to the revert transaction which is enough to indicate that the “git revert” command has functioned properly. However, we can still ensure it by checking the Git status. Here, we will be able to see that there will be no more files to be committed as the ones that were there previously i.e. Stash1 and Stash2 have already been deleted by the revert operation. Therefore, there will be no more new files to be committed as shown in the following image:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/word-image-321.png" data-lazy- height="151" src="data:image/svg xml,” width=”531″>

Also, we can try to list down the contents of our Git project repository to confirm the deletion of the files Stash1.txt and Stash2.txt as shown in the image below. A point to be noted over here is that the files Stash1.txt and Stash2.txt have been deleted by our revert operation solely because before committing the transaction having the transaction ID of 220ac, there was no existence of these two files. That is why, as a result of reverting this commit, these files will not exist anymore. However, if you will perform the revert operation once again on this revert transaction i.e. you will try to nullify the effect of the revert operation you have just performed, then you will be able to see these two files again.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/word-image-322.png" data-lazy- height="75" src="data:image/svg xml,” width=”460″>

Finally, we can also check our file abc.txt once again to see if it is back with its original content or not. We will simply open this file again with the nano editor and you will notice from the following image that the modification that we did earlier to this file has been reverted.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/word-image-323.png" data-lazy- height="576" src="data:image/svg xml,” width=”731″>

Conclusion:

By going through the detailed scenario presented to you in this article, you will hopefully be able to use the “git revert” command in Ubuntu 20.04 in a very effective manner. This command will cancel the effect of any previous transaction while maintaining its entry within the “git log” so that at any point in time, you can conveniently go back to that transaction. In other words, we can say that the “git revert” command commits a transaction that is an exact inverse of a command that was committed previously (without deleting the previous commit). That is exactly why it manages to cancel its effect.

About the author

<img alt="Karim Buzdar" data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/karim-150×150.png5fe91e7c2b7e9.jpg" height="112" src="data:image/svg xml,” width=”112″>

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.