To revert a commit in Git, you can use the git revert command. This command creates a new commit that undoes the changes made in the specified commit, effectively reverting it.
Experiment: <git revert commit> with examples
For this tutorial, we will use a git repository that is already existing. If you want to learn to add files to your repository, you can check this page: Git add and Git commit examples
Step #1. Go to your git repository and check the git status
ubuntu@codetryout:~/codetryout-code$ git status
On branch master
nothing to commit, working tree clean
We have a few commits, and here is the list of files. Each of them was created for each commit, for a better understanding.
ubuntu@codetryout:~/codetryout-code$ ls -l
total 0
-rw-rw-r-- 1 ubuntu ubuntu 0 Jan 5 22:01 file1
-rw-rw-r-- 1 ubuntu ubuntu 0 Jan 5 22:03 file2
-rw-rw-r-- 1 ubuntu ubuntu 0 Jan 5 22:03 file3
Step #2. Git revert your latest commit
As per the git log, your latest commit id is 6815e845f13046570aaccdb66aee38caa68a6a60
git revert <COMMIT-ID>
These are the steps to revert the commit:
ubuntu@codetryout:~/codetryout-code$ git revert 6815e845f13046570aaccdb66aee38caa68a6a60
Removing file3
[master 9a97f1b] Revert "commit 3"
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 file3
Let us verify whether our latest changes have been reverted (file3 should not be there)
ubuntu@codetryout:~/codetryout-code$ ls -l
total 0
-rw-rw-r-- 1 ubuntu ubuntu 0 Jan 5 22:01 file1
-rw-rw-r-- 1 ubuntu ubuntu 0 Jan 5 22:03 file2
ubuntu@codetryout:~/codetryout-code$
Step #3. Let’s revert one more file
ubuntu@codetryout:~/codetryout-code$ git revert efc13573a8b93ce10d74980b0ad4ac510e63734b
Removing file2
[master 62cb3f9] Revert "commit 2"
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 file2
ubuntu@codetryout:~/codetryout-code$
Verifying the latest revert:
ubuntu@codetryout:~/codetryout-code$ ls -l
total 0
-rw-rw-r-- 1 ubuntu ubuntu 0 Jan 5 22:01 file1
ubuntu@codetryout:~/codetryout-code$
Step #4: Let us explore the git log to see all changes, commits, and reverts
Look at them closely, the commits and reverts have been updated. You may reverse these changes in the future.
ubuntu@codetryout:~/codetryout-code$ git log
commit 62cb3f9682c74cdca237c5a1c9533ecb521b6628 (HEAD -> master)
Author: Ubuntu <[email protected]>
Date: Tue Jan 5 22:31:46 2021 +0530
Revert "commit 2"
This reverts commit efc13573a8b93ce10d74980b0ad4ac510e63734b.
commit 9a97f1b9091524f31e969f4d0ab65f118b2af137
Author: Ubuntu <[email protected]>
Date: Tue Jan 5 22:27:03 2021 +0530
Revert "commit 3"
This reverts commit 6815e845f13046570aaccdb66aee38caa68a6a60.
commit 6815e845f13046570aaccdb66aee38caa68a6a60
Author: Ubuntu <[email protected]>
Date: Tue Jan 5 22:03:36 2021 +0530
commit 3
commit efc13573a8b93ce10d74980b0ad4ac510e63734b
Author: Ubuntu <[email protected]>
Date: Tue Jan 5 22:03:20 2021 +0530
commit 2
commit b36945a7bc5df78259a97a6bc6c90224e56c1434
Author: Ubuntu <[email protected]>
Date: Tue Jan 5 22:02:54 2021 +0530
commit 1
ubuntu@codetryout:~/codetryout-code$
Note: This test case is done in an Ubuntu terminal, however the same will work on any Git command line, such as Git Bash, Linux/CentOS and Docker containers with git client, etc.
After following these steps, the changes introduced by the commit you reverted will be undone, and a new commit will be created to record the revert. This approach is useful when you want to keep a record of the reversion while preserving the commit history.