Preheat the oven to 375 degrees and lightly grease an 8-inch square baking dish.
Melt the butter in a saucepan over medium heat. Add the minced garlic and sliced onion, then cook until the onion softens.
Stir in the flour and cook for one minute. Slowly whisk in the heavy cream, salt, pepper, and thyme until the sauce thickens slightly.
Spread half the sliced potatoes in the baking dish. Scatter half the diced ham over the potatoes and pour half the cream sauce on top.
Repeat the layers with the remaining potatoes, ham, and sauce. Sprinkle the shredded cheddar cheese evenly over the top.
Cover the dish with foil and bake for 30 minutes. Remove the foil and bake for another 20 minutes until the potatoes are tender and the cheese is golden.
Let the gratin rest for 10 minutes before cutting into portions.