Quitting the game and starting up again seems to have done the trick.
My ship was back to 6 little health icons when I did that.
Note: In this case my game actually crashed (because PS4), so I'm only assuming that quitting the game would have had the same effect.
(The save point that reloaded was from a couple minutes before the game crash, which was several hours after my ship was damaged.)
gaming.stackexchange.com/users/50264/dg99
At the very least, you can talk to the NPC in a space station, and providing you have the appropriate rank with them you can ask them for repairs, which should repair your ship.
gaming.stackexchange.com/users/145420/SGR
I've noticed when docked at a planet based trade station (a multi platformed one) that when I went back to my ship the damage was repaired.
Earlier in the same play session I'd stopped at a space station in hopes of recovering ship health and it didn't happen.
I don't often get into ship combat, and when I do I usually die, so this is the only instance of ship repair I can account for.
Not sure if this was the actual cause for repair or if traveling a good distance (shields maxed, like how your health recovers) had played a factor into it, but I'm pretty sure it only happened after visiting the planet trade station.
If you encounter a similar experience I'd love to know.
Hope this helps.
gaming.stackexchange.com/users/164211/Ebril
Park in a space station and let it autosave.
Then restore current saved game and your ship health will be full.
gaming.stackexchange.com/users/167212/SleepyInsomniac
Aside from the answers given by the other answerers, warping also seems to repair ship health damage, which is fine if you aren't interested in exploring that system.
gaming.stackexchange.com/users/150779/LiveMynd
If my shield (or my hazard protection) fails and my body takes damage, I can get health from all sorts of things: wall dispensers, shiny artifacts, plants, helpful aliens, etc.  What about when my ship's shields fail and it takes damage?  How can I heal that?  (My current ship started out with 5 or 6 little ship icons below its shield bar; now it has only 2.)
How do I heal my ship in No Man's Sky?
gaming.stackexchange.com
<no-mans-sky>
gaming.stackexchange.com/questions/281382
gaming.stackexchange.com/users/50264/dg99
Park in a space station and let it autosave.  You can also quit the game and start up again. Try quitting the game and starting it again. Or park in a space station and autosave. Warping also does the same.
Users suggest different methods to heal the ship