You can not undo any data overwritten on any storage media. But you can try to recover data that is not overwritten from the external hard drive. Just pick up professional data recovery software to scan the target drive, perhaps, you can get some files back.
The next time your MacBook won't turn on, you can recover data from the unbootable Mac in macOS Recovery Mode to get wanted files off the drive and proceed to fix the unbootable Mac without losing data.
According to your description, you have selected the iMac hard drive in Disk Utility and clicked Restore, and chosen Retore From the MacBook hard drive, therefore the iMac hard drive was erased, and a copy of the MacBook hard drive is kept on the erased iMac hard drive, unfortunately, you can't undo this action.
Any data overwriting is irreversible, the previous data on the iMac hard drive is replaced by new data. Have you ever backed up the iMac hard drive with Time Machine, iCloud, or in other ways? If your answer is YES, you can restore the iMac hard drive with your backups.
Besides, given the data is stored in the sectors randomly, perhaps some previous files on the iMac hard drive haven't been overwritten, then you can recover such data back. You'd better recover them ASAP to avoid further data overwritten. What you need is data recovery software such as iBoysoft Data Recovery for Mac, it can scan the drive for lost/deleted files, then you can preview and recover wanted files to a different location. Just download it and try it for free now!