Question: is it grammatically correct to put a comma before but?
Answer: You should put a comma before but only when but is connecting two independent clauses. I would go for a walk, but it's raining outside. ... That means they're independent clauses, so you need to use a comma before but. When you don't have two independent clauses, leave the comma out.