I know, logically, that over-analysing your headaches and migraines can make the situation worse. You spend your whole life "in your head", trying to work out solutions to problems that may not have any. But it can also be comforting to know, and even befriend "the devil".
Below, I offer some examples from my own experience, with the following conclusion: as long as it is a conscious choice, problem solving and analysis is a positive force. But if you are trapped in your own thoughts, I'd recommend mindfulness meditation. This will over time equip you with the skills to make it a conscious choice.
1. Analysing your headaches and migraines can be comforting
I've kept a daily migraine and headache diary for more than 5 years in a spreadsheet, which allows me to look at patterns, trends and triggers. Since I love spreadsheets and analysis, this has been a fun distraction from the pain itself. I have also found some really valuable insights that allow me to manage the condition better. E.g., I now know the 2 days in a month when I'm bound to get a headache due to hormones, and accepting this reduces the fear and aggravation significantly.
2. You can get caught up in over-analysis that doesn't ultimately lead anywhere
For years, I have had a hypothesis that having (a large amount of) something sugary will cause a headache the following day. I call it the "sugar hangover". In fact, I can remember this happening more than 20 years ago, well before I started keeping my headache and migraine diary. However, recently I have concluded that actually, your body and mind are a lot more complex than this! There are at least 4 possible explanations, and despite mulling over this and trying to conduct empirical experiments with myself, I now think I will never know which one is the right one.
a) I could be imagining the link; the fact that I think I will get a headache from sugar actually brings it on the following day (a kind of negative placebo effect)
b) It could in fact be true that sugar causes a headache: after all, it can cause a spike, and then a crash, in blood sugar levels; and for me, I feel it also is dehydrating; of course, NOT having it is not an easy headache management strategy, either!
c) The causality could be the other way round! Maybe when I am tempted to reach for sugar (normally in the form of chocolate), my body already knows there's a headache coming, and the sugar is a form of painkiller I have subconsciously used for all these years. [Note: when I already have a headache, chocolate or sugary sweets definitely do work -- often better than normal painkillers!]
d) Or it could be that the same thing that is causing my cravings for sugar (let's say, hormones or stress) are the things that also cause a headache later on, but there is no causal link directly between sugar and headaches.
All of this speculation is no doubt a bit of a waste of time and energy, so my last point is about how to side-step it, if you want to.
3. Mindfulness meditation can help you notice when your own thinking is making you worse off
I won't go into a huge amount of detail, but highly recommend Jon Kabat-Zinn's books and methodology. Through this, I've learned to notice that a lot of this thinking and analysis actually makes me feel worse, not better. It has also taught me techniques to "get out of the head" reliably, whenever I notice I need it. Overall, it has helped me accept that maybe there are no answers to the many questions headaches and migraines raise, and that "not knowing" is absolutely fine. For an over-analytical person used to finding solutions to the most intractable problems, this is not an EASY thing to accept -- but it is necessary, if I want to avoid a vicious cycle of over-thinking and stress about the headaches and migraines making the very condition worse.