Piloting your starship is quite the balancing act in Starfield as you manage shifting power between shields, weapons, and engines. The best way to avoid taking ship damage is to stay away from space battles completely—if you're quick enough, you can just grav jump away and avoid death in the void—but if you do take damage, here's how to repair it on the fly, or get it back into shape after the fact.
How to fix your ship
If you get caught in a conflict and manage to escape relatively unscathed, your best port of call a hub area like New Atlantis where a ship technician will be able to restore your ship to full working order for 1,000 credits. It's here that you'll also be able to buy new ships and upgrade your existing one with new parts or a new look if you desire.
If you decide to stay in a skirmish, however, you have the option to heal your ship as you continue the flight by hitting O or clicking the right stick on gamepad. However, you can't just heal your ship using nothing—you'll need to have ship parts available, which you can find in the Aid section of your inventory menu.
It's important then to search for resources when a fight is over or when you've destroyed even one enemy ship by looting it for ship parts that you can use in the second half of the fight or once it's over.
Where to get ship parts
You can buy ship parts from general merchants for around 1225 credits each. They have a mass of 10.00, so make sure you store them in your ship's cargo as soon as possible. You can also loot ship parts from damaged vessels, whether you were a part of the fight or not, and parts will also be moved to your storage after you upgrade your ship.