Humans have always had a urge to cover their heads with various forms of headgear. There must be a hat gene in our genomes and it probably evolved in the human lineage after it split from chimpanzees. Chimps don't wear hats.
If you believe this then you've probably fallen for the idea that FOXP2 is a gene for language and that there's a gene for God/religion. See The hunt for the Hat Gene for a good discussion of where you're going wrong.