Create a disk image for recovery
In Windows there is a concept of creating a "disk image" for recovery, where one creates a complete copy of the operating system drive so that if the operating system doesn't work, the disk image can be reloaded into the computer, effectively bringing the computer to the state it was when the image was created. This is done with a rescue CD or flash drive - the flash drive is connected, the computer is started up, thereby booting from this drive, and the software on the drive can then reload the disk image from an HDD or wherever that image is located.
I'm looking for the same functionality in a Mac but can't find it, perhaps because I'm using the wrong term for it. I'm new to the Mac world, and the term "disk image" seems to mean something different in that world.
So how do I get this recovery functionality (-recover to a previous state from a completely unusable operating system) with a Mac?
Boot to the Recovery HD by holding ⌘R on boot. Choose Disk Utility, select your system partition from the sidebar, then select File → New Image → Image from "<partition name>". Follow the instructions to choose a save location.
To restore your image, boot to Recovery, create a partition of equal size or larger than the image you saved (if necessary), then select that partition and choose the Restore button on the toolbar.
The options are greyed out in the above screenshots as I am not booted to Recovery.
The Recovery System is part of macOS and should already be a separate partition on your startup disk (hidden in Disk Utility). You can use Internet Recovery with ⌥⌘R on most Macs to perform a NetBoot-esque recovery using Apple's server, for example if you have a fresh HDD that you want to restore on to.
If you want to put a Recovery System on an external disk or secondary internal disk, you can use Recovery Disk Assistant (1 GB required).