For thousands of years India, China, and other Asian regions have had the majority of Earth's population, so throughout history European military practices have been minority practices anyway.
But I will look at European warriors and soldiers first. European male - and thus warrior hairstyles - changed many times.
The really ancient Greeks wore their hair long. In classical times hair was usually shorter. Spartan boys wore their hair short but grew it long as men. Athenian boys wore long hair, cut it short at puberty, and then longer again in manhood. Ancient Roman men wore their hair long, but wore it short in classical times.
During many, but not all, generations and centuries of the Byzantine Empire, men wore their hair long. Maybe Byzantine soldiers also wore their hair long.
This coin depicts Jesus Christ on one side and Emperor Nikephoros II Phocas (r.963-969) a famous warrior emperor, and his stepson Emperor Basil II (r. 960-1025), a famous conquering emperor known as the Bulgar-slayer", on the other.
In the middle ages, nobles in many European cultures and many eras wore their hair long, and were also the main fighting force.
King Richard I the Lionheart of England (reigned 1189-1199), England's most famous warrior King, wore his hair long:
King Henry III of England (r. 1216-1272) had long hair:
King Edward III of England (r.1327-1377) had long hair:
Here is a portrait of King Richard II of England (reigned 1377-99).
Henry V (reigned 1413-22) had a short haircut.
King Henry VI (reigned 1422-1461 & 1470-71) has a third hair style in this portrait:
King Edward IV (1461-70 & 1471-83) has his hair long in his portrait.
King Richard III (1483-85):
King Henry VII Tudor (reigned 1485-1509):
Henry VIII (r. 1509-1547) began a group of short-haired monarchs:
King Charles I (r. 1625-1649) began another group of long-haired monarchs:
But later on English monarchs began to wear wigs made of other people's hair.
Rear Admiral Augustus Brine (1769-1840) had his portrait painted as a midshipman in 1782. Note the long hair he wore as an officer in training:
In short, over many centuries hair styles change and the length of men's hair changes, and there have been many times when it was normal for warriors in western society to have long hair.
In your science fictional society there would not seem to be any reason for spaceship crews to have hair of any specific length, if spaceships have artificial gravity and there is no danger of long hair floating into exposed machinery (in Star Trek the machinery is usually hidden behind panels). I have seen hundreds of Star Trek episodes including many space battles, and I don't remember many situations where a character having very long hair would have made it more dangerous for him.
And you can always provide a justification for space crews having long hair in the era of your story by having characters look at images of famous space officers from the past and laughing at their several different funny hairstyles from different eras of the UTF Navy, showing that styles have changed over and over in the future and long hair has happened to come into fashion in the era of your story. Have the uniform styles change several times in the images of past space heroes, too.