Tutorial: How to revert your git commit

This example shows how to revert your git commit.

As an example I have modified README.md file in my website

C:\code\smccake\smccake>git diff
diff --git a/README.md b/README.md
index 44ea578..58e71fc 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,2 @@
-# smccake
+# smccake test
St. Marys church Cake Project

Committed the code with “git commit command”

C:\code\smccake\smccake>git commit -m "test commit" -a
[main 0c92a65] test commit
 1 file changed, 1 insertion(+), 1 deletion(-)

C:\code\smccake\smccake>git log
commit 0c92a659288e78cd277acd57ec5bdb8aec37464e (HEAD -> main)
Author: Yogindar <yogindar@yahoo.com>
Date:   Thu Feb 3 16:12:41 2022 +0530

    test commit

Note the commit hash id 0c92a659288e78cd277acd57ec5bdb8aec37464e. This is what we used to refer back our commits.

Now we wanted to revert the commit, we can use the “git revert” command with the commit hash.

C:\code\smccake\smccake>git revert 0c92a659288e78cd277acd57ec5bdb8aec37464e
[main 5c26ac1] Revert “test commit”
1 file changed, 1 insertion(+), 1 deletion(-)

You can do a “git push”

I’m going to give you another extra lecture on how to add the reverted changes back.

So we know the commit hash, now generate the diff again using the commit hash.

Create a patch with the diff and apply it.

C:\code\smccake\smccake>git diff “0c92a659288e78cd277acd57ec5bdb8aec37464e^” 0c92a659288e78cd277acd57ec5bdb8aec37464e
diff –git a/README.md b/README.md
index 44ea578..58e71fc 100644
— a/README.md
+++ b/README.md
@@ -1,2 +1,2 @@
-# smccake
+# smccake test
St. Marys church Cake Project

git diff “0c92a659288e78cd277acd57ec5bdb8aec37464e^” 0c92a659288e78cd277acd57ec5bdb8aec37464e > changes.patch

C:\code\smccake\smccake>git apply changes.patch

C:\code\smccake\smccake>git diff
diff –git a/README.md b/README.md
index 44ea578..58e71fc 100644
— a/README.md
+++ b/README.md
@@ -1,2 +1,2 @@
-# smccake
+# smccake test
St. Marys church Cake Project

Happy giting…:-)

About the Author

Yogi

24 years of experience in various layers of software. Primarily experienced in system side software design and development on server management software. Interested in linux development in x86 and arm architecture.