My first child started head banging and it freaked me out. She did it because she was mad if I took something away from her, specifically the TV remote. She would throw herself down and start banging her head on the brick fireplace hearth. The first time, I was terrified she was going to hurt herself or get brain damage, so I picked her up to comfort her. She knocked a knot on her head and it obviously hurt. You'd think that would be the last time. Well, it wasn't. The second time she did it, I stood her up on her feet and popped her on the seat of her pants. She never saw THAT coming! I picked her up and put her in her crib and said, "You do NOT hit your head! NO!"
She cried but I left her in there for about 5 minutes and went in and got her and told her, "I love you. You don't hurt my baby." I kissed her head and told her no more owies.
She never banged her head again. She still got mad that I wouldn't let her play with the remote, but that amounted to fussing and crying about it which I let go in one ear and out the other. She never hit her head again though.
Some say to let them do it because when it hurts enough they'll quit, but my daughter was hard headed in more ways than one and I just wasn't going to ignore her banging bruises and lumps on her forehead. And, I didn't want to comfort her for throwing a fit. So...I swatted her.
I know some people won't agree with that, but a swat on her diapered tush hurt her far less than banging her head.
It worked for her. My son never banged his head. Thank God.