
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 a file in git named example.txt that contains multiple commits. The git log will show both commits and provide you with the commit ID.
~]# git log example.txt
commit 0f1d8505d3f6751df020c5fb35dcf291704d6896
Author: John Doe <john.doe@example.com>
Date: Thu Oct 17 16:29:23 2024 -0500
replaced World with Earth
commit ccbce960aaad2208dfea2eb683de19ef559cb3b7
Author: John Doe <john.doe@example.com>
Date: Fri Aug 12 05:20:55 2023 -0500
replaced Hello with Goodbye
commit 3d7940db29f80acf676199e9cb49efd88d2e2227
Author: John Doe <john.doe@example.com>
Date: Fri Aug 12 05:18:31 2022 -0500
initial commit
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. Almost always, this is used to revert the latest commit, which in this example would be the commit 0f1d8505d3f6751df020c5fb35dcf291704d6896 that replaced World with Earth.
If HEAD is the latest commit in the currently selected branch, git revert HEAD can be used to revert the latest commit to the prior commit.
git revert HEAD
Or, you can specify a specific commit using the commit ID, with the exception of the very first commit, which is the bottom most commit in the git log output.
git revert 0f1d8505d3f6751df020c5fb35dcf291704d6896
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 "replaced World with Earth" is used which reverts to the prior commit, which is the commit that contains "replaced Hello with Goodbye".
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 0f1d8505d3f6751df020c5fb35dcf291704d6896
1 file changed, 1 insertion(+), 1 deletion(-)
Or --no-commit can be used to revert to the prior commit but to not actually commit the revert.
git revert --no-commit 0f1d8505d3f6751df020c5fb35dcf291704d6896
In this scenario, the git status command would then show that the revert is staged but not yet committed.
~]$ git status
On branch master
Your branch is up to date with 'origin/master'.
You are currently reverting commit 0f1d8505.
(all conflicts fixed: run "git revert --continue")
(use "git revert --skip" to skip this patch)
(use "git revert --abort" to cancel the revert operation)
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: example.txt
This is nice because then you can use the git commit command to create a commit with your own commit message.
git commit -m "my custom revert message" example.txt
So, you can combine git revert --no-commit and the git commit command as a way to revert a commit without having to enter a message into your default editor.
git revert --no-commit 0f1d8505d3f6751df020c5fb35dcf291704d6896; git commit -m "my custom revert message" example.txt
Likewise, the git log will contain a new commit and your commit message.
~]$ 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
If you have multiple commits that you would like to revert, you first need to revert the latest commit, and then the second latest commit, and so on, until you are back to the version of the file that you want.
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