Short answer: macOS has no switch for this, but you can reassign Quit to a shortcut you will never hit by accident, in System Settings.
The steps are below, along with why Q of all keys, and what to do about the tabs you already lost. Everything here applies to Cmd W as well, which is the key you were actually aiming for.
Command Q quits the whole app. Every window, every tab, all of it, straight away and with no confirmation unless a document has unsaved changes. Command W, the key right next to it, closes only the front tab, which is the difference that catches people out.
No. There is no undo for quitting an app, and that is the honest answer to the question most people arrive here asking. Once the app is gone, macOS has nothing to reverse. What you can do is get some of it back:
What does not come back is anything typed into a web form, an unsaved scratch file, or the exact arrangement of windows you had. That is the part people actually mind, and it is why stopping the quit is worth more than any recovery trick.
Look at where the keys are. Q sits directly above A and immediately left of W. Three of the most-used shortcuts on the Mac live on that cluster:
One is a rounding error away from the other two, and only one of them is catastrophic. Miss W by a few millimetres and Safari does not close a tab, it closes everything: every window, every tab, and any form you had half filled in.
Nothing warns you. There is no undo. That is the whole problem, and it is a design decision Apple has never revisited.
You cannot switch Cmd Q off globally, but you can move it, per application, so the dangerous shortcut is no longer under your little finger.
Quit Safari.Cmd Q now does nothing in that app. The menu updates to show the new combination.
Two things that trip people up. The menu title has to match character for character, including the app name, so it is "Quit Safari" and not "Quit". And you have to repeat this for every app you want protected, which is the main weakness of the approach.
Worth doing regardless, because it turns a disaster into an inconvenience.
In Safari, open Settings, then General, and set Safari opens with to All windows from last session. Quit by accident and reopening puts every window and tab back.
It does not recover text you had typed into a form, so it is a safety net rather than a fix.
Both already ship the guard that Safari lacks.
That these browsers all added it independently tells you how common the complaint is. Safari is the outlier.
The other approach is to make Command Q require intent, by adding a short delay so a tap does nothing and only a deliberate hold quits. That covers every app at once.
Several tools do this, including free ones. It is the right shape of solution: you keep the shortcut you already know, it just stops firing on a graze.
Still Dock, which locks the Mac Dock, has quit protection as a switch in the menu bar, alongside the Dock locks, so the same app that stops your Dock changing by accident also stops your apps closing by accident. Same idea, applied to the other thing macOS lets you destroy in one keystroke. On a Mac other people use, that switch can also be flipped from your phone: see stop ⌘Q quitting apps on a shared Mac, from your phone.
Using more than one screen? The Dock also jumps between them, which is a separate problem with its own fixes: how to keep the Mac Dock on one screen.
Not in System Settings. The App Shortcuts panel works per application, so a global change needs a utility that sits between the keystroke and the app.
Usually one of three things. The app is busy or unresponsive, so it never receives the keystroke, and force quit from the Apple menu is the way out. Or a document has unsaved changes and a save dialog is waiting behind another window. Or the shortcut has been reassigned, either by you in Keyboard Shortcuts or by an app that guards it, in which case the File menu shows what Quit is now bound to.
No. Quit still works, from the menu or from your new shortcut. You have moved the shortcut, not removed the command.
macOS matches App Shortcuts against the literal text of the menu item. "Quit Safari" and "Quit" are different strings, and a mismatch simply does nothing, with no error to tell you why.
In apps that use the standard document model, yes, you get a save prompt. Browsers and many others just close, which is why the browser case hurts most.
Reopen the app and check its History menu, and in Safari use History then Reopen All Windows From Last Session. Text typed into a form is usually gone.
Chrome and Firefox each have a built-in guard for this that costs nothing to turn on. See stop ⌘Q quitting Chrome, Safari and Firefox.