anquegi anquegi - 26 days ago 12
Git Question

can git not commit a file if the file contains certain words?

Maybe this question is repeated but I couldn't find anything.

Let's suppose I'm developing with ruby and I'm debugging a file with byebug, so in that file I have the word "byebug".

I find the problem and save that correct file but I forget to remove the byebug word. so when I commit that file, it will be commited with that word, than can cause in production the app not working well.

I'm using magit and emacs, so before committing I always can read the diff and look for the "byebug" word. If there it is I can remove.

I also know that I can do something similar to

git diff | grep byebug
and detect if there is the bybug word, But I want to know if this can be possible only with using git, or the correct way to this automatically with git

Answer

What you are looking for is called Git hooks. Hooks are scripts that are run at different stages (pre-commit, post-commit) and can prevent the action to succeed.

I think that in your case the client-side pre-commit hook is what you are looking for. An useful introduction to git hooks https://www.digitalocean.com/community/tutorials/how-to-use-git-hooks-to-automate-development-and-deployment-tasks

Comments