You can revert a single commit in git using git revert <commit>
, below is a step by step guide on how to do this.
Step by step
We start by creating the following file in our work space:
and commit and push it:
git add .\my-test-file.txt
git commit -m "adding a file"
git push
We then create a second change:
and commit and push it:
git add .\my-test-file.txt
git commit -m "making mistakes"
git push
Let us say that we want to revert our last change so the file looks like our first commit. We can use git log
to get an overview of our commits:
commit bc404b9051e5405e092f66c26ed28f5392240c55 (HEAD -> master, origin/master, origin/HEAD)
Author: Peter <email>
Date: Fri Mar 3 22:44:50 2023 +0100
making mistakes
commit 12c828e4ed03531f7a4a4a3e58860c52d169cfe4
Author: Peter <email>
Date: Fri Mar 3 22:44:14 2023 +0100
adding a file
We can use the command git revert
to revert the changes of bc404b9051e5405e092f66c26ed28f5392240c55
like the following:
git revert bc404b9051e
This will create the following commit:
commit b7c641868c5a8ddc1fec0a82a2f523f6e6f93991 (HEAD -> master, origin/master, origin/HEAD)
Author: Peter <email>
Date: Fri Mar 3 22:50:46 2023 +0100
Revert "making mistakes"
This reverts commit bc404b9051e5405e092f66c26ed28f5392240c55.
Where the file is looking like from before bc404b9051e
:
You can then push this and the file and commit is reverted.
That is all
I hope you found this helpful, feel free to leave a comment down below!