When having brain surgery the doctors do shave your head before the surgery is performed.
You can also shave off your hair on your head yourself before the surgery is performed but usually the doctors will come in and shave your head and remove most if not all of your hair prior to the brain surgery.
Sometimes the hair only needs to be shaved part way and other times the hair on your head needs to be completely shaved off.
If you're having brain surgery for tumors in the brain stem or back part of the brain then usually only a small portion of your hair needs to be shaved to perform the brain surgery.
But in other cases of brain surgery the hair on your head usually needs to be completely shaved off.