Skip to content

Instantly share code, notes, and snippets.

@hraban
Last active April 18, 2024 06:46
Show Gist options
  • Star 64 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save hraban/10c7f72ba6ec55247f2d to your computer and use it in GitHub Desktop.
Save hraban/10c7f72ba6ec55247f2d to your computer and use it in GitHub Desktop.
Prevent accidentally committing debug code in Git
@doronguttman
Copy link

Thank you @hraban !
if this does not work (line #13), try git config --global core.hooksPath "$HOME/.git_template/hooks" instead

@hraban
Copy link
Author

hraban commented Aug 22, 2022

@doronguttman I have looked into this but as far as I can tell there is no clean way of "adding" a hook. By setting a global hooksPath, you are locked in to that dir for all repos. You can override it locally, but now you lose all global hooks, and you're back to copy/pasting hook scripts from the template dir.

Conversely, if you have any existing repos with custom hooks, setting that global option will disable them.

The best approximation of "supplemental hooks" I've found is appending if [[ -x .git/hooks/pre-commit ]]; then exec ./git/hooks/pre-commit; fi but it feels brittle.

@doronguttman
Copy link

Thanks for your reply @hraban, for me, the init.templatedir just did not work 🤷‍♂️ , but the core.hooksPath solved the issue for me. Maybe others can use the init.templatedir.
In any case, your work here is highly appreciated ❤️

@hraban
Copy link
Author

hraban commented Aug 24, 2022

Ah now I understand what you meant, you had a specific problem getting it to actually work at all. Interesting, are you on Windows? What do you think the problem could be? Just for completeness' sake: the templatedir only applies to new repositories, not to existing ones. Might that have been the problem.

Either way, it's a gist, all comments welcome :P it's 3 lines of bash--not exactly ffmpeg 😂

@doronguttman
Copy link

😄 indeed on windows (wsl debian), but I guess my issue was the misunderstanding of what templatedir actually is. Indeed I was looking for it to work on an already cloned repo. I guess I had missed this comment 😊

well, maybe someone will find the core.hooksPath comment useful 🤷‍♂️

@netslow
Copy link

netslow commented Feb 11, 2024

why there is symbol d in regex ^[+d].*NOCOMMIT ? In which cases diff can return lines starting with d?

@hraban
Copy link
Author

hraban commented Feb 11, 2024 via email

@netslow
Copy link

netslow commented Feb 11, 2024

ok, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment