Preventing Git Commits Containing Temporary Code

I've built a workflow that prevents me from pushing temporary code via git.

Did you ever find legacy debugging or testing code in your projects, e. g. something like the following?

                            
console.log(foo); // just for testing
                            
                        

Let's take my personal website as an example: Whenever I push local changes to the main branch of my GitHub repository, my server pulls the newest changes.

I love this workflow – but I have to be very careful because of three problems:

  • Problem 1: Nobody reviews code changes.

  • Problem 2: I am far from 100% automatic test coverage.

  • Problem 3: I can't invest several hours at a time with full concentration.

I believe many small, personal and closed source projects share at least some of those problems.

So what's your solution?

I can't change any of the problems above, but I had a simple idea – let's have a look on the next example:

                            
console.log(foo); // do-not-commit
                            
                        

I decided to set a simple keyword (e. g. "do-not-commit") as a comment, whenever I write temporary code – and combined it with a Git hook, that recognizes this keyword and prevents me from committing.

How did you realize your idea?

  1. I installed git-secrets globally on my computer. This is a git extension (Linux, Windows, Mac) that rejects commits if they match any configured RegEx pattern.

  1. I'm using a Mac, therefore I have to install and configure git secrets in every repo using the following code:

                            
git secrets --install
git secrets --add --literal 'do-not-commit'
                            
                        

From now on I get the following error message, as soon as the keyword is part of any commit.

                            
[ERROR] Matched one or more prohibited patterns
                            
                        

Any other thoughts on this process?

To save some seconds, I've created a global text replacement in macOS preferences that replaces "dnc" with "do-not-commit".

Of course I have to remember to set the keyword – but for me, that feels more safe than to remember every debugging code. 🙃