Short answer
It is a sunnah of habit. The Prophet ﷺ wore his hair between his earlobes and his shoulders, sometimes longer, sometimes shorter. A man who grows his hair to follow him is rewarded, but short hair or a shaved head is also allowed. The Hanafi texts say the sunnah for the head is either to keep the hair with a parting or to shave it all.
Keeping the hair like the Prophet ﷺ, to the earlobes or shoulders with a middle parting, is a sunnah of habit, and al-Tahawi described shaving the whole head as sunnah too. Long hair styled like a woman's must be avoided.
The four schools agree on this.
Explanation
Anas described the Prophet's ﷺ hair as neither tightly curled nor straight, falling between his ears and his shoulders, and in another report it touched his shoulders. Aisha said it was longer than the earlobes and shorter than the shoulders. So its length varied, and he combed it, oiled it and parted it in the middle.
Is it a sunnah? Yes, of the habitual kind. The Hanafi scholars say the sunnah for the head is either to keep the hair with a parting or to shave it all. If you grow your hair out of love for the Prophet, you are rewarded. If you keep it short, you are doing nothing wrong. Neither choice by itself makes someone a better Muslim. The four schools agree that both are allowed.
Conditions. Keep it clean and combed; see the Sunnah of hair care. Do not style it like a woman's hair, with hair clips or women's braids, and do not shave part and leave part; see haram haircuts. Many Hanafi muftis advise men not to let it grow far past the shoulders, since that starts to look like women's hair.
Prayer and wudu. A man should not pray with his hair tied up in a knot or bun, so let it down before you begin; see can men braid their hair. In wudu, wipe at least a quarter of the head itself, not only the hair hanging below it.
The evidence
Quran, Surah Aal-i-Imraan 3:31
قُلْ إِن كُنتُمْ تُحِبُّونَ ٱللَّهَ فَٱتَّبِعُونِى يُحْبِبْكُمُ ٱللَّهُ وَيَغْفِرْ لَكُمْ ذُنُوبَكُمْ ۗ وَٱللَّهُ غَفُورٌۭ رَّحِيمٌۭ ﴿٣١﴾
Say, [O Muhammad], "If you should love Allah, then follow me, [so] Allah will love you and forgive you your sins. And Allah is Forgiving and Merciful."
Following the Prophet out of love, even in habits, is rewarded.
Sahih al-Bukhari 5905
Sahih... ليس بالسبط ولا الجعد بين أذنيه وعاتقه ...
"It was neither straight nor curly, and fell between his ears and his shoulders."
Describes the length of the Prophet's hair.
Sunan Abi Dawud 4187
Grading to be checked... كان شعر رسول الله صلى الله عليه وسلم فوق الوفرة ودون الجمة ...
"The hair of the Messenger of Allah was longer than the earlobes and shorter than the shoulders."
Another description of his usual length.
Sahih al-Bukhari 5903
Sahih... كان يضرب شعره منكبيه ...
"His hair used to reach his shoulders."
At times it reached the shoulders.
Where the ruling is recorded
- Radd al-Muhtar (Ibn Abidin), Kitab al-Hazr wa al-Ibaha. The sunnah for the hair of the head is either parting it or shaving it, with al-Tahawi's report on shaving.
- al-Fatawa al-Hindiyya, Kitab al-Karahiyya, chapter on circumcision, hair and nails. On keeping and shaving the hair of the head.
Checked by our team. For a more accurate answer to your own situation, consult a scholar. Quran translation by Sahih International. Hadith Arabic from the standard editions of the six books and the Muwatta, with gradings as recorded there; the English is our own rendering. Spotted a mistake? Tell us.