I am not sure if there's a word for that, but sometimes you may imagine yourself doing something stupid and then twist and shake your head for a brief second, what is that movement called? Is there even a word for it? It doesn't have to be the neck and the head only, because I am not sure how other people "physically" react.
A slight, quick, repeated motion is a jitter. Larger repeated movements are a shake or wag. Shaking and wagging are slower: shaking the head is a rotation at the neck, and wagging is a shoulder-to-shoulder lateral movement of the head.
She jittered her head in disbelief.
Astounded, I wagged my head.
All of these apply to movements of any body part, not just the head.
Referring to the head swimming usually refers to a feeling of lightheadedness from dizziness or intoxication but could describe certain back-and-forth motions of the head.
My head swam for a moment to show them I realized how dopey I must have sounded saying that.
Another that applies to the head — the mind, really — and may be the best fit is the idiom shake the cobwebs loose. Having recently woken up, fatigued at the end of the day, or preoccupation with other matters may slow one’s mental faculties.
‘What a stupid thing to say,’ I thought and rattled my head to shake the cobwebs loose.
I need a moment here to shake the cobwebs loose.
Depending on the level of surprise or shock, more than just the head may be involved. Owain's answer includes start and jerk. These are single, unrepeated movements. A related single movement is startle used as a verb.
He startled, unable to believe what he had just witnessed.
When hit with utter shock or surprise, all or part of the body may be taken aback: rolling the head backward (perhaps with raised eyebrows), bending backward at the neck and waist, or even taking a step backward. Similar expressions are blown away and took my/his/her breath away. All of these are used figuratively to express extreme surprises, but taking the breath away can have the connotation of romance and falling in love.
Billy was taken aback by what she revealed.
I am just blown away with disappointment.
Emphasizing the Effect
Disappointment in oneself or others, surprise, and so on may provoke movement of the head or other physical responses, and we do it because we are boggled or to regain composure.
His mind boggled, he slowly shook his head in an effort to regain his composure.
1a : to move suddenly and violently : spring started angrily to his feet
b : to react with a sudden brief involuntary movement : started when a shot rang out
2 : a single quick motion of short duration
4a : an involuntary spasmodic muscular movement ...