Preheat the oven to 400 degrees. Grease a 9x13 baking dish with butter and set aside.
Peel and thinly slice 3 pounds of russet potatoes. The slices should be about ⅛ inch thick for even cooking.
In a medium saucepan, melt the butter over medium heat. Add the sliced onions and sauté until translucent and soft, about 5-7 minutes. Add the minced garlic and cook for an additional minute until fragrant.
Add the flour and stir to coat to make the roux. Slowly whisk in the flour until there are no more lumps. Add the salt and pepper then continue to cook until the sauce begins to thicken.
Remove the sauce from heat and add in the shredded cheese. Stir until completely melted and smooth.
Arrange a layer of potato slices in the bottom of the prepared baking dish. Pour a spoonful of sauce over the layer making sure it's evenly covered. Add another layer of potatoes then sauce and continue the layers until all the ingredients are used. Add any leftover cheese to the top.
Cover the dish with aluminum foil and bake in the preheated oven for 40 minutes. Remove the foil and bake for an additional 15 minutes or until the top is golden brown and bubbly, and the potatoes are tender when pierced with a knife.