During a tour this spring at Tesla Inc.’s electric-car factory in Fremont, Calif., Elon Musk asked why the assembly line had stopped. Managers said automatic safety sensors halted the line whenever people got in the way.

Mr. Musk became angry, according to people familiar with what happened. His high-profile gamble on mass-producing electric cars had lagged behind since production began, and here was one more frustration. The billionaire entrepreneur began head-butting the front end of a car on the assembly line.

“I...