Preheat the oven to 190C/170C fan/Gas 5. Butter a medium ovenproof dish.
Melt half the butter in a pan over low heat. Add the sliced onions and cook gently for 15 minutes until very soft and golden. Add the garlic and thyme for the last minute.
Layer half the sliced potatoes in the bottom of the dish. Season with salt, pepper and a grating of nutmeg. Spread the caramelised onions over the top.
Layer the remaining potatoes on top. Season again. Pour over the milk.
Dot the remaining butter over the top and cover with foil. Bake for 30 minutes.
Remove the foil, sprinkle over the grated cheese and bake for another 15 minutes until the top is golden and bubbling and the potatoes are tender when pierced with a knife.