2

When I am trying to run git commit -m 'message here' I am getting following error.

fatal: cannot exec '.git/hooks/prepare-commit-msg': Permission denied

This issue started after when I created a new partition on my ubuntu and cloned the repo in it.

Sushmit Sagar
  • 1,412
  • 2
  • 13
  • 27

3 Answers3

8

You'll need to make your file executable, the below code snippet will make the file executable for the owner, the group and the world:

$ chmod +x .git/hooks/prepare-commit-msg
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
  • 1
    Is the OP probably talking about an older version of git? With git 2.29.2 I'm getting `hint: The '.git/hooks/prepare-commit-msg' hook was ignored because it's not set as executable.` and `git commit` still succeeds. Or is it a shell-related issue, not git? – terrorrussia-keeps-killing Dec 21 '20 at 14:58
  • 2
    @fluffy I can see that a check for non executable files was introduced in 2.13 see the commit https://github.com/git/git/commit/940283101ce87250cf31a592730386f5061e1286 I'm not sure which version OP is running. – Andreas Louv Dec 21 '20 at 15:13
  • 3
    The message was added later, in 2.14.3 https://github.com/git/git/commit/f805a00a396ee91599902cebe55620b2a4c813b9 – Andreas Louv Dec 21 '20 at 15:15
  • I can see this phenomenon too, and I'm wondering where this hook is originating from, what creates it and why it is missing the step of making it executable. Any idea? – Hendrik Wiese Oct 13 '21 at 05:23
  • @HendrikWiese You created the file, either by creating it manually, by having used the `init.templatedir`, (`--templatedir`) when cloning / initializing a repository. Or if you have run a program that creates these hooks. – Andreas Louv Oct 13 '21 at 09:31
  • I think it's the latter. I'm just wondering which program that might be... I'm using GitKraken as git UI, IntelliJ IDEA/PyCharm as IDE... one of them _might_ be the culprit... – Hendrik Wiese Oct 13 '21 at 12:41
  • Or `chmod +x .husky/pre-commit` if you are using `husky`. – SalahAdDin Mar 13 '22 at 10:16
0

For me, chmod +x for those git hooks did not help and I had to remove the file (move it to {filename}.bak).

I suppose this isn't a great solution, though.

tedro
  • 21
  • 4
0

For me I was getting the error fatal: cannot exec '.git/hooks/pre-push': Permission denied.

Applying Andreas's answer to this error message, I tried $ chmod +x .git/hooks/pre-push, and that fixed it. The subsequent commit and push commands went through successfully.

Apparently such fatal: cannot exec ... : Permission denied errors involving both git hooks can be resolved in the same way.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34705247) – Koedlt Jul 23 '23 at 20:26