Sometimes you add changes to the repository that you do not want, or should not, send to the server. Two commands come to the rescue - git revert and git reset.
Reverting a commit
If you do not mind that the Git log will show the commit and then the fact that it was undone (or you even want it that way), git revert is your friend. The idea is simple - it undoes changes by reversing them. If commit A adds the line echo $string;, revert does the opposite - it creates a commit B that removes that line (exactly what you could do by hand).
Let's make a bit of a mess and use git revert to undo the last commit (giving its hash):

After a push to the server, the removal is visible in the log. Nothing wrong with that; I think it is an elegant way to "clean up" without breaking the continuity of the history. If you sent the changes to the server and someone has already pulled them, they just pull again to get the changes made by git revert, because it is just another commit. With a hard removal (covered in the next section), pulling from the server would mean overwriting other people's local repositories.
Removing a commit
Sometimes you commit changes you do not want to keep in the history at all (for whatever reason). If you have not sent them to the server yet (push), you can remove them with git reset:

As you can see, the commit is gone from the log, and the changes from that commit are back in the working tree. You can now restore them, fix them and commit again, or drop them.
If you do not want the commit's changes in the working tree, use the command with the --hard flag (the default is --mixed):

There is a third, gentlest option too: git reset --soft. It removes the commit but keeps its changes ready to commit (in the staging area). Handy when you only want to fix the commit message or add one more file to it.
Moving a commit to another branch
It happens a lot: you are on the main branch, say main (master in older repositories), fix a bug and accidentally commit locally to it instead of first creating a separate "topic" branch for the pull/merge request. To keep your work and clean up the current branch, first create a new branch, then reset the main one.
So let's use two commands, git branch new-branch-name and git reset --hard HEAD~1, to "move" the last commit to the new branch and remove it from the old one:

Undoing changes you have not committed yet
Newer versions of Git have a separate, readable command for this, git restore:
# Bring a file back to its state from the last commit (the changes are lost!)
git restore index.php
# Take a file out of the staging area (git add), the changes stay
git restore --staged index.php
Before that, you did it with git checkout -- file and git reset file. Those still work, but restore says plainly what it does.
When things go wrong: git reflog
A removed commit does not disappear straight away. Git remembers where HEAD pointed over the last weeks, and git reflog shows that list. Find the hash from before the mistake and go back to it:
git reflog
git reset --hard a1b2c3d
Careful with git push --force. If you remove a commit that was already on the server, a normal push will fail. Forcing it rewrites the history for everyone else on the team too. If you really must, use git push --force-with-lease, which refuses if someone pushed something in the meantime. On a shared branch, git revert is the safer choice.