Remember, try to fix it first, then panic.
As more sites require accounts and passwords to access, it can become difficult to remember all of them. Most internet browsers have built-in password managers these days, which helps, but there are some passwords that your computer can’t track for you, and they’re usually the most important ones. Your Mac login credentials, for example, usually aren’t saved, and if you forget them, it may feel like you’ve been completely locked out of your computer. Not to worry, though, there are a few options you can use to get in.
If your Apple ID is linked to your Mac (and it should be), you’ll have the easiest time. If you get your password wrong a few times, you’ll be prompted for your Apple ID email and password. As long as you haven’t forgotten those, you can use your account to reset your computer’s password.
If your Apple ID isn’t linked, it’s gonna be a little trickier, but you still have options. If you happen to share your computer with other users and you aren’t the admin, then whoever is the admin can log in and reset your password for you. This is accomplished in the “Users & Groups” section of the system preferences.
If that admittedly really specific situation doesn’t apply to you, there’s one more trick you can try: recovery mode. Turn your Mac off, and hold the command and R keys before pressing the power button. This’ll get you into recovery mode. Click on “Utilities” in the menu bar, and then click “Terminal.” This’ll give you a command prompt you can use to enter direct commands to the OS. Simply type “resetpassword” (one word, no quotes) and press the return key. The Reset Password tool should pop up, and from here you can reset user passwords. Once you’ve changed the password, power down again and reboot normally. You should be able to log in now. Remember to write down that new password, especially if you share the computer with others!