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.
https://en.wikipedia.org/wiki/Nikephoros_II_Phokas#/media/File:Histamenon_nomisma-Nicephorus_II_and_Basil_II-sb1776.jpg1
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:
https://en.wikipedia.org/wiki/Richard_I_of_England#/media/File:Church_of_Fontevraud_Abbey_Richard_I_effigy.jpg2
King Henry III of England (r. 1216-1272) had long hair:
https://en.wikipedia.org/wiki/Henry_III_of_England#/media/File:Henry_III_funeral_head.jpg3
King Edward III of England (r.1327-1377) had long hair:
https://en.wikipedia.org/wiki/Edward_III_of_England#/media/File:Edward-III-king-England.jpg4
Here is a portrait of King Richard II of England (reigned 1377-99).
https://en.wikipedia.org/wiki/List_of_English_monarchs#/media/File:Richard_II_King_of_England.jpg5
Henry V (reigned 1413-22) had a short haircut.
https://en.wikipedia.org/wiki/List_of_English_monarchs#/media/File:Henry5.JPG6
King Henry VI (reigned 1422-1461 & 1470-71) has a third hair style in this portrait:
https://en.wikipedia.org/wiki/Henry_VI_of_England#/media/File:King_Henry_VI_from_NPG_(2).jpg7
King Edward IV (1461-70 & 1471-83) has his hair long in his portrait.
https://en.wikipedia.org/wiki/List_of_English_monarchs#/media/File:EdwardIVofEngland-Yorkist.jpg8
King Richard III (1483-85):
https://en.wikipedia.org/wiki/Richard_III_of_England#/media/File:Richard_III_earliest_surviving_portrait.jpg9
King Henry VII Tudor (reigned 1485-1509):
https://en.wikipedia.org/wiki/List_of_English_monarchs#/media/File:King_Henry_VII_from_NPG.jpg10
Henry VIII (r. 1509-1547) began a group of short-haired monarchs:
https://en.wikipedia.org/wiki/Henry_VIII_of_England#/media/File:Workshop_of_Hans_Holbein_the_Younger_-_Portrait_of_Henry_VIII_-_Google_Art_Project.jpg11
King Charles I (r. 1625-1649) began another group of long-haired monarchs:
https://en.wikipedia.org/wiki/List_of_English_monarchs#/media/File:King_Charles_I_after_original_by_van_Dyck.jpg12
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:
https://commons.wikimedia.org/wiki/File:Midshipman_Augustus_Brine.jpg13
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.