Always keep the temperature of the pan on medium to low flame this will prevent garlic from burning.
If you cook at a very high flame then garlic will burn from the outside but remain uncooked from the inside.
Always try to add garlic at the end of sauteing onions when the onions will starts translucent and browning gradually.
This process helps to roast the garlic perfectly without sticking to the pan.
Saute garlic in very high smoke points oils, such as avocado oil, canola oil, or grapeseed oil.
Because these oils can withstand higher temperatures which help to prevent the garlic from burning.
While sauteing chopped garlic you have to be more cautious because smaller garlic pieces may burn instantly in hot oil and alter the taste of the dish.
While cooking to prevent garlic from burning make sure stir the garlic frequently.
This will help to cook garlic evenly , also prevents it from burning
You can use a thick bottom pan which helps you to prevent garlic from burning.
Because it helps to distribute heat evenly around the pan and reduces the chances of burning the garlic.