git revert is the most recommended and common way, although there are a few other ways to do it too, and it can be useful to be aware of them.
When you use
git revert, you aren’t really “undoing” the commit. Rather, you’re creating a new commit that’s the inverse of the commit in question. This means you can still checkout the old commit and see the contents of it, even if master no longer has the contents. This is typically what you want, because it’s safe and you don’t loose the previous information that was there.
However, let’s say you committed something that shouldn’t be public, like a private API key or a password. In this case, you don’t want that information to still be in the commit history. Much of the time you can’t do anything about this, but there are some very specific circumstances where you can. If you have not pushed the commit in question to GitHub or your repository hosting service of choice, then you have more flexibility. You can run
git reset --soft HEAD~[number of commits you want to undo], e.g.
git reset --soft HEAD~1 to undo the latest commit. This does remove the commit from history, unlike
git reset can be dangerous. If you have pushed your code anywhere else, and then you run this command, you can get into some really strange and very hard to fix edge cases if anyone else is working on this repo with the commit you just removed. So be sure that you only use this command if you have not pushed your code anywhere else.