Why is my Git pre-commit hook not executable by default?

If you see the accepted answer in: Aggregating and uglifying JavaScript in a Git pre-commit hook, you'll see that I had to do a chmod +x on my pre-commit hook to get it to work.

Why is this not executable by Git by default?


Solution 1:

Because files are not executable by default; they must be set to be executable.

The sample files from a git init are all executable; if it's copied or renamed to a non-sample file, it will retain the original file's x flag.

New files will be created with current defaults. In your case, view those defaults with umask:

$ umask
0022

By default, new files won't be u+x unless explicitly set to be.

Solution 2:

My context - web development Node.js. I needed to add husky functionality, but got an error, probably to the disk access. This helped in my situation:

chmod ug+x .husky/*
chmod ug+x .git/hooks/*