A useful trick we've had at previous workplaces which solves this (and related accidental-debug-code-checkin) problems is to put a precommit hook that denies a checkin with a certain string in the text (we used "DNCI" for Do Not Check In)
So you get in the habit of annotating test tweaks & debug prints with:
printf("DNCI x is %d\n", x);
I use gdb as well, but sometimes dumping data directly is all you really need.