You don't have to avoid them entirely, but moderating your intake of gas-inducing foods can help prevent gas-related headaches.