One of the strategies I like to use for debugging is printing logs. Once debugging is done, removing all the debug logs is a big hassle. If I am not careful, I could accidentally commit and upload the logs for review! The embarrassment! And what if I am not done with debugging and need to upload the code to share with team cause I am stuck?
After going through the struggles mentioned above few times, I started to think if there was any way to make git ignore my debug logs. Turns out there is! 🎉
You use git filters to achieve this. The purpose of git filter is to simply clean your code from sensitive information, but I will hack it and remove all my logs.
You can create a new filter by adding the following to your git config file at $XDG_CONFIG_HOME/git/config
:
[filter "removesamiullog"] clean = sed '/SAMIUL/d'
And then turn on the filters for all files you are interested in at $XDG_CONFIG_HOME/git/attributes
:
*.java filter=removesamiullog
Once you add the above configurations, anytime you add or commit files, git will run the custom filter on the files. The sed command finds any line in the file with string “SAMIUL” and deletes it.
So none of these lines will get added to git:
Log.d("SAMIUL", "This will get deleted by git"); foo(); // SAMIUL
This allows me to add debug logs without worrying about them getting leaked into the git. Saves a lot of time too.