This shaving applies only to boys. As for girls, the Messenger of Allah ﷺ forbade a woman to shave her head. Hence, when a child is born, the boy’s head is shaved on the seventh day along with the ʿaqīqa (the sacrificial offering), whereas the girl’s head is not shaved.
These ḥadiths also indicate that wearing one’s hair long is not a sunnah. “Wearing the hair” means allowing the hair of the head to grow until it becomes thick and hangs down in a plait or as locks. It is simply a customary practice. Had it been a sunnah, the Prophet ﷺ would have said, “Leave it; do not shave the boy’s head,” and he would not have ordered that the heads of Jaʿfar’s children be shaved. Rather, growing the hair is no more than a social habit: if the people of your region customarily let their hair grow, then do so; if they do not, then do not.
Some scholars have judged it to be a sunnah, but that is their ijtihād. The sound view is that it is not a sunnah, and we do not instruct people to grow their hair. We say: if society is accustomed to long hair, grow it so that you do not stand out; if people are not accustomed to it—as is presently the case among us—then do not. For this reason our senior teachers—such as Shaykh ʿAbd al-Raḥmān ibn Saʿdī, Shaykh Muḥammad ibn Ibrāhīm, Shaykh ʿAbd al-ʿAzīz ibn Bāz, and others—did not wear their hair long, because it is not a sunnah but merely a custom. Allah alone grants success.
A girl’s hair is not to be shaved, whether she is young or grown, except out of necessity—for example, if her head is wounded and requires medical treatment, then there is no harm. The Prophet ﷺ, when he needed cupping while in a state of ihram, shaved part of his head and had the cupping performed, even though shaving the head of a muḥrim is ordinarily prohibited; necessity is a different matter.