The most commonly repeated speculation of the origin of this expression is that English archers in medieval times used to store spare bowstrings under their hats to keep them dry.
- Firstly, keeping dry isn't keeping secret, so even if archers did store strings under their hats, and there's no evidence that they did, where is the connection to the phrase's meaning?
- Secondly, and it would have been kinder to put this first as it entirely dismisses the archer tale, the phrase isn't known in English until the 19th century - so much for a medieval origin.
The phrase didn't derive from putting anything under one's hat at all - 'under your hat' simply meant 'in your head'.
The extended phrase 'keep it under your hat', which didn't arise until the 20th century, simply meant 'keep it in your head.' An early example is found in P. G. Wodehouse's Inimitable Jeeves, 1923:
It made such a hit with her when she found that I loved her for herself alone, despite her humble station, that she kept it under her hat.