Heat the oven to 375°F (190°C). Grease a 2-quart gratin or similar baking dish with the unsalted butter.
Rub the dish all over with the cut sides of the garlic, then drop those cloves into a medium saucepan with the fresh thyme, heavy cream, whole milk, nutmeg, kosher salt, and black pepper.
Bring the dairy mixture just to a simmer over medium heat, stirring, then reduce heat and simmer gently for 3–5 minutes. Remove from heat and let steep 10 minutes, then fish out and discard the garlic and thyme.
Peel the Yukon Gold potatoes (if desired) and slice 0.125 inch (3 mm) thick; do not rinse. If working ahead, keep slices covered to prevent browning.
Arrange half the potato slices in the prepared dish, overlapping slightly. Pour over half the infused dairy and sprinkle evenly with half the Gruyère cheese.
Repeat with remaining potatoes, the rest of the dairy, and the remaining Gruyère. Press down gently so the top layer is moistened and mostly submerged. Sprinkle the Parmesan cheese evenly over the top. Cover tightly with foil.
Bake covered for 40 minutes, then uncover and bake 20–30 minutes more until the potatoes are very tender when pierced and the top is deep golden and bubbling.
Let rest 10–15 minutes before serving so the gratin sets and slices cleanly.