The British Armed Forces are also know as "Her Majesty's Armed Forces" (now His Majesty's). The official head of the armed forces is the monarch and that's who they swear their allegiance to. However, there is a long standing constitutional convention that the executive authority is given to the Prime Minister by "royal perogative". So technically the monarch, but in reality it's the Prime Minister.
If the monarch tried to actually do something significant with that power I imagine the law would be changed pretty quickly.
This is why working royals generally have various military titles and positions, because the monarch is the head of the armed forces. It's also why the monarch dresses in military regalia for various military events.