The expression “to keep one’s hair on” means: To restrain one’s temper; to remain calm and serene, unruffled despite provocation.
This was popular American slang of less than a century ago.
In fact, the earliest literary example appears to have been in Dr. Claudius, one of the first pieces of fiction written by Francis Marion Crawford.
As with most slang, a positive source of the expression cannot be determined. However, it could have reflected the Indian raids upon isolated homesteaders that were still taking place in some of the western territories.
Settlers and wagoners who could remain unexcited in the face of any such raid were the most likely to be able to fight off a threatening horde and thus to retain their scalps.
This is, of course, conjecture.
Neither Crawford nor other writers of that period, the early 1880’s, left any clue that would suggest the source.