Bootstrap FreeKB - Git (Version Control) - Roll back to a prior commit using the git revert command
Git (Version Control) - Roll back to a prior commit using the git revert command

Updated:   |  Git (Version Control) articles

The most basic way to use Git is to use the git clone command to clone an origin Git repository (such as example.git) to a directory on your PC (such as /home/john.doe/git), make a change to a file in the cloned repository on your PC (such as example.txt), use the git commit command to commit the change to the file, and to then use the git push command to upload the file to the origin Git repository.

 

Let's say you've added and commited example.txt to the currently selected branch of the cloned repository and example.txt contains "Foo".

~]# echo Foo > example.txt
~]$ git add example.txt
~]$ git commit -m "example.txt contains Foo" example.txt
[master 3d7940d] testing
 1 file changed, 1 insertion(+)
 create mode 100644 example.txt

 

Then you replaced Foo with Bar and commit example.txt.

~]$ echo Bar > example.txt 
~]$ git commit -m "example.txt contains Bar" example.txt
[master ccbce96] example.txt contains bar
 1 file changed, 1 insertion(+), 1 deletion(-)

 

The git log will show both commits and provide you with the commit ID.

~]# git log example.txt
commit ccbce960aaad2208dfea2eb683de19ef559cb3b7
Author: John Doe <john.doe@example.com>
Date:   Fri Aug 12 05:20:55 2022 -0500

    example.txt contains Bar

commit 3d7940db29f80acf676199e9cb49efd88d2e2227
Author: John Doe <john.doe@example.com>
Date:   Fri Aug 12 05:18:31 2022 -0500

    example.txt contains Foo

 

The git diff command can be used to confirm that the commits contain differences.

]$ git diff ccbce960aaad2208dfea2eb683de19ef559cb3b7:example.txt 3d7940db29f80acf676199e9cb49efd88d2e2227:example.txt
diff --git a/ccbce960aaad2208dfea2eb683de19ef559cb3b7:example.txt b/3d7940db29f80acf676199e9cb49efd88d2e2227:example.txt
index cf96eb3..5cebaa2 100644
--- a/ccbce960aaad2208dfea2eb683de19ef559cb3b7:example.txt
+++ b/3d7940db29f80acf676199e9cb49efd88d2e2227:example.txt
@@ -1,6 +1,6 @@
-  Foo
+  Bar

 

Likewise, the git show command can be used to confirm that the file had Foo at first commit and Bar at second commit.

]$ git show ccbce960aaad2208dfea2eb683de19ef559cb3b7:example.txt 
Bar

]$ git show 3d7940db29f80acf676199e9cb49efd88d2e2227:example.txt
Foo

 

It's a good idea to use git stash, in case example.txt has commits that have not been pushed to the origin Git repository.

git stash

 

The git revert command can be used to to roll back to a prior commit. HEAD is usually the latest commit in the currently selected branch, thus the git revert HEAD will revert the latest commit to the prior commit. In this example, this will roll back from the current commit (ccbce960aaad2208dfea2eb683de19ef559cb3b7) that contains "Bar" to the prior commit (3d7940db29f80acf676199e9cb49efd88d2e2227) that contains "Foo".

git revert HEAD

 

Or, you can specify a specific commit using the commit ID, with the exception of the very first commit.

git revert ccbce960aaad2208dfea2eb683de19ef559cb3b7

 

AVOID TROUBLE

When reverting, you do not select the commit that you want to go back to. Instead, you select the commit that you want to revert. Notice in this example that the commit that contains "Bar" is used which reverts to the prior commit, which is the commit that contains "Foo".

By default, your default editor will open and you will be prompted to enter the commit message. You may want to use the --no-edit option to bypass this prompt.

~]# git revert --no-edit ccbce960aaad2208dfea2eb683de19ef559cb3b7
 1 file changed, 1 insertion(+), 1 deletion(-)

 

Likewise, the git log will contain a new commit ID followed by keyword Revert, and your commit message. Notice that this means that revert also does a commit, thus there is no need to do a commit.

~]$ git log example.txt
commit 57f03ffa9d4350c908a8718a6d0b173471da929e
Author: John Doe <john.doe@example.com>
Date:   Fri Aug 12 05:23:07 2022 -0500

    Revert "example.txt contains Bar"
    
    This reverts commit ccbce960aaad2208dfea2eb683de19ef559cb3b7.

commit ccbce960aaad2208dfea2eb683de19ef559cb3b7
Author: John Doe <john.doe@example.com>
Date:   Fri Aug 12 05:20:55 2022 -0500

    example.txt contains Bar

commit 3d7940db29f80acf676199e9cb49efd88d2e2227
Author: John Doe <john.doe@example.com>
Date:   Fri Aug 12 05:18:31 2022 -0500

    example.txt contains Far

 

Don't forget to push the file from your local Git repository (branch) to the master Git repository (trunk).

git push origin HEAD

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter f86d16 in the box below so that we can be sure you are a human.