Walking through "I committed to the wrong branch"
The most common git mistake is also a good lesson in how git works. You meant to start a new branch for a fix, forgot, and committed straight onto main. You have not pushed yet. git log --oneline shows the new commit, say f3a9c21 "Fix login timeout", on top of main's previous commit, say 8b1e0d4. The goal is to have the fix on its own branch and main back where it was.
The recipe is three commands, run on main. First, git branch fix-login-timeout. That creates a new label pointing at the current commit, f3a9c21, but leaves you on main. Your fix is now safe on a branch, whatever happens next. Second, git reset --hard HEAD~1. HEAD~1 means one commit before the current one, which is 8b1e0d4, so main moves back there and your files change to match. Third, git switch fix-login-timeout, and your fix is in front of you again, on the branch where it belongs.
It is worth checking each step, and the commands to do it are cheap. After the first step, git log --oneline --all shows both main and fix-login-timeout pointing at f3a9c21. After the second, main points at 8b1e0d4 while the new branch still points at f3a9c21. Nothing was deleted: the commit is still there, it just has one label instead of two. That is the key idea behind most git rescues. Branches are labels, and moving a label never destroys a commit while another label, or the reflog, still points at it.
There are two warnings, and the recipe on this page says them too. git reset --hard throws away any changes you have not committed, so commit or stash them first. And if you had already pushed main, rewriting it would cause trouble for everyone who pulled it. In that case, create the branch the same way, then on main run git revert f3a9c21 and push. That adds a new commit undoing the fix on main, while the branch keeps it.
This page has honest limits. It covers the commands most people use every week, not all of git, which has well over a hundred commands and thousands of options. It does not run anything: it only explains and copies. The explanations describe git 2.23 and later, which is what any computer updated in the last few years has. And when a situation is truly tangled, such as a rebase halfway through with conflicts, git status and git reflog will tell you more about your own repository than any cheat sheet can.