3

I've been working on a new feature for my team's project, and my code changes involved many debug print statements on rather useful places. Now before pushing the code, I'll be removing all these debug print statements but I know whenever I come back to this I'll end up putting those debug print statements again.

So if I remove the debug statements, and commit. All the needed debug statements will basically be git diff HEAD^!. Inverse of this diff (if that's even a thing) will be all the debug statements I need whenever in need. Is there a way to do that?

Or would you suggest any other sane workflow to handle this kind of a thing.

user1265125
  • 2,608
  • 8
  • 42
  • 65

2 Answers2

4

If your prtinf statements are easily parsed, you can declare a content filter driver, here a clean filter:

http://git-scm.com/book/en/v2/book/08-customizing-git/images/clean.png

(Source: Pro Git book: Customizing Git - Git Attributes)

Your clean script will be a simple sed command skipping lines matching a pattern.

That way, any git diff or git commit will consider your sources without the printf debug statements.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

This can be done, for example, with git stash -p approach, where you can manually select what to stash and what not, but I'd rather not recommend do that, since this stash will be very fragile and when you'll do a couple of updates from master (from other developers maybe) your stash won't be applied cleanly and you'll have to resolve conflicts which can be more troublesome than just adding those debug statements by hand.

As an alternative, you can make those debug statements conditional, for example, run them only if the code is being run on your machine, if some DEBUG environment variable is set or something:

puts 'my debug here' if ENV['DEBUG'] # example in ruby

This way these debug statements will be seen only on your machine and not in production.

Alexey Shein
  • 7,342
  • 1
  • 25
  • 38