We can use git revert
It is an “undo” command, but technically it is much more than that. Git revert does not delete any commit in this project history. Instead, it inverts the changes implemented in a commit and appends new commits with the opposite effect.
Let’s see the steps:
Identify the Commit: Find the hash of the commit you want to revert by running:
git log
Revert the Commit: Use git revert with the commit hash:
git revert <commit-hash>
You can replace <commit-hash> with the hash of commit you want to undo.
Git will create new commit which will undergo the changes of the specified commit.
If there will be any error , git will inform you and you need to resolve that error first.
Finalize the Revert: Once conflicts are resolved, add the changes and complete the revert with:
git commit