How to fix error: cannot run .git/hooks/commit-msg: No such file or directory in Git

GitBEGINNERLOWUpdated Dec 6, 2025ยท 152 views

This error occurs when Git tries to execute a commit-msg hook that doesn't exist or has an invalid configuration. The hook file may be missing, have incorrect permissions, contain an invalid shebang line, or reference a non-existent interpreter. The fix involves verifying the hook file exists and is properly configured.

Quick fix

Fixes error: cannot run .git/hooks/commit-msg: No such file or directory

# List all hooks
ls -la .git/hooks/

# Check specifically for commit-msg
ls -la .git/hooks/commit-msg
  1. Check if the hook file exists
  2. Remove or fix the broken hook configuration
  3. Fix shebang and encoding issues
  4. Bypass the hook temporarily
git โ€” error
# List all hooks
ls -la .git/hooks/
# Check specifically for commit-msg
ls -la .git/hooks/commit-msg
error: cannot run .git/hooks/commit-msg: No such file or directory

What this error means

The "cannot run .git/hooks/commit-msg: No such file or directory" error indicates that Git is attempting to execute a commit-msg hook script that either doesn't exist or cannot be found by the system. This error blocks your commit because Git is configured to run this hook but cannot locate the executable. Git hooks are scripts that run automatically at certain points in your Git workflow. The `commit-msg` hook specifically runs after you write a commit message but before the commit is finalized. It's commonly used for: - Validating commit message format (conventional commits) - Adding ticket numbers or prefixes automatically - Enforcing commit message standards across a team The error typically occurs when: - **Hook management tools like Husky were uninstalled** but left configuration behind - **The hook file references a missing interpreter** (wrong shebang line) - **The hook was manually deleted** but Git configuration still references it - **File encoding issues** prevent proper parsing of the hook script - **The hook file lacks execute permissions** (though this usually produces a different error) This is a configuration problem, not a Git bug. Your repository's hooks configuration expects a script that the system cannot locate or execute.

Typical symptoms

  • Git commit fails with 'cannot run .git/hooks/commit-msg: No such file or directory'
  • Commits worked before but suddenly fail after removing a package or tool
  • Error appears after cloning a repository that uses Husky or pre-commit
  • The error mentions a specific path like '.git/hooks/commit-msg' or '.husky/commit-msg'
  • Running 'npm install' or 'yarn install' doesn't fix the issue
  • The error persists even after creating an empty commit-msg file

Common causes

  1. Husky or another Git hook manager was uninstalled but left configuration behind
  2. The hook file has a shebang line referencing a non-existent interpreter (e.g., #!/usr/bin/python when Python isn't installed)
  3. The hook file is missing from the .git/hooks or .husky directory
  4. File encoding is UTF-8 with BOM instead of plain UTF-8 (common on Windows)
  5. The shebang line has whitespace or an empty line before it
  6. core.hooksPath in Git config points to a non-existent directory
  7. A symlink in .git/hooks points to a deleted file
  8. The hook file exists but has Windows line endings (CRLF) causing the shebang to fail

How to fix "error: cannot run .git/hooks/commit-msg: No such file or directory"

1Check if the hook file exists

First, verify whether the commit-msg hook file actually exists:

Check the standard hooks directory:

bash
# List all hooks
ls -la .git/hooks/

# Check specifically for commit-msg
ls -la .git/hooks/commit-msg

Check if Husky is configured:

bash
# Look for Husky directory
ls -la .husky/

# Check for commit-msg in Husky
ls -la .husky/commit-msg

Check Git's hooks path configuration:

bash
git config core.hooksPath

If this returns a path (like .husky), Git is looking for hooks in that directory instead of .git/hooks.

2Remove or fix the broken hook configuration

Based on what you found, choose the appropriate fix:

Option A: If Husky was uninstalled but config remains

bash
# Remove the custom hooks path
git config --unset core.hooksPath

# Or remove it globally if set globally
git config --global --unset core.hooksPath

Option B: If the hook file exists but is broken

bash
# Remove the broken hook
rm .git/hooks/commit-msg

# Or if using Husky
rm .husky/commit-msg

Option C: If you want to keep using hooks, reinstall Husky

bash
# Install and initialize Husky
npm install husky --save-dev
npx husky init

# Or with yarn
yarn add husky --dev
npx husky init
3Fix shebang and encoding issues

If the hook file exists but has formatting issues:

Check the shebang line:

bash
# View the first line of the hook
head -n 1 .git/hooks/commit-msg

The first line MUST be a valid shebang with no leading whitespace:

bash
#!/bin/sh

or

bash
#!/bin/bash

Fix encoding issues (Windows):

bash
# Check for BOM (will show characters before #!/bin/sh)
file .git/hooks/commit-msg

# Convert to Unix line endings
dos2unix .git/hooks/commit-msg

# Or use sed
sed -i 's/\r$//' .git/hooks/commit-msg

Fix permissions:

bash
chmod +x .git/hooks/commit-msg

Create a minimal working hook if needed:

bash
cat > .git/hooks/commit-msg << 'EOF'
#!/bin/sh
exit 0
EOF
chmod +x .git/hooks/commit-msg
4Bypass the hook temporarily

If you need to make a commit immediately while troubleshooting:

Skip all hooks for one commit:

bash
git commit --no-verify -m "Your commit message"

The --no-verify flag (or -n for short) tells Git to skip pre-commit and commit-msg hooks.

Caution: This bypasses any validation the hook was performing. Use sparingly and fix the underlying issue rather than making this a habit.

Alternative - temporarily disable hooks globally:

bash
# Disable hooks
git config core.hooksPath /dev/null

# Make your commit
git commit -m "Your commit message"

# Re-enable hooks
git config --unset core.hooksPath
5Clean up package.json scripts

If the issue stems from a removed Husky installation, clean up your package.json:

Check for leftover Husky configuration:

json
{
  "scripts": {
    "prepare": "husky install"  // Remove this if Husky is uninstalled
  }
}

Remove from package.json if Husky is no longer used:

bash
# Remove the prepare script
npm pkg delete scripts.prepare

# Or manually edit package.json

If you're keeping Husky, ensure it's properly installed:

bash
# Fresh Husky setup (v9+)
npm install husky --save-dev
npx husky init

# This creates .husky/pre-commit
# Add commit-msg hook if needed:
echo "npx --no -- commitlint --edit $1" > .husky/commit-msg
6Fix team-wide hook issues

If this error affects your whole team after cloning:

For projects using Husky:
Make sure Husky is in devDependencies and the prepare script is set:

json
{
  "devDependencies": {
    "husky": "^9.0.0"
  },
  "scripts": {
    "prepare": "husky"
  }
}

After cloning, team members should run:

bash
npm install
# This triggers the prepare script and sets up hooks

For projects using pre-commit (Python):

bash
pip install pre-commit
pre-commit install

Document hook setup in your README so new team members know how to set up their environment.

Advanced notes

Understanding Git Hooks Architecture:

Git hooks live in .git/hooks/ by default, but this can be changed with core.hooksPath. When you install Husky, it typically sets:

bash
git config core.hooksPath .husky

This redirects Git to look in .husky/ instead of .git/hooks/ for all hooks.

Why "No such file or directory" When the File Exists:

This error is misleading. It often means:
1. The shebang interpreter doesn't exist (#!/usr/bin/python3 but Python 3 isn't installed)
2. The file has CRLF line endings making the shebang invalid (#!/bin/sh\r is not a valid path)
3. The file has a UTF-8 BOM making the shebang unparseable

To diagnose:

bash
# Check what the system thinks the file type is
file .git/hooks/commit-msg

# Should say: "POSIX shell script" or similar
# If it says "ASCII text" with no script type, shebang is broken

# Check for hidden characters
cat -A .git/hooks/commit-msg | head -1
# Should show: #!/bin/sh$
# Bad: ^M at end (CRLF) or weird characters at start (BOM)

Hook Inheritance in Monorepos:

In monorepos, hooks are tricky because .git is at the root but you may run Git from subdirectories. Husky handles this, but if you remove Husky, the core.hooksPath setting might still point to a non-existent directory relative to where you're running Git.

Debugging Hook Execution:

bash
# See exactly what Git is trying to execute
GIT_TRACE=1 git commit -m "test"

This shows the full path Git attempts to use for hooks.

Platform-Specific Issues:

Windows:
- Ensure Git Bash is available if hooks use #!/bin/bash
- File permissions work differently; Git tracks the executable bit
- Use .cmd or .exe extensions for Windows-native scripts
- WSL and Windows Git can conflict if you mix them

macOS:
- /bin/bash is Bash 3.x (old); for newer features use /usr/local/bin/bash or /opt/homebrew/bin/bash
- Homebrew-installed interpreters have different paths than system ones

Linux:
- Most flexible; standard paths usually work
- Some distros don't have /bin/bash (use #!/usr/bin/env bash for portability)

The `#!/usr/bin/env` Trick:

For maximum portability, use:

bash
#!/usr/bin/env bash

Instead of:

bash
#!/bin/bash

The env approach finds the interpreter in your PATH, which works across different systems where interpreters might be installed in different locations.

Security Considerations:

Git hooks can execute arbitrary code. Be cautious when:
- Cloning repositories from untrusted sources (hooks in .husky are in the repo)
- The .git/hooks directory is not in the repo, so traditional hooks don't spread
- Husky hooks ARE in the repo, which is convenient but requires trust

Review hook contents before running npm install on untrusted repositories.

Frequently asked questions

What does "error: cannot run .git/hooks/commit-msg: No such file or directory" mean?
git/hooks/commit-msg: No such file or directory" error indicates that Git is attempting to execute a commit-msg hook script that either doesn't exist or cannot be found by the system. This error blocks your commit because Git is configured to run this hook but cannot locate the executable. Git hooks are scripts that run automatically at certain points in your Git workflow.
How do I fix "error: cannot run .git/hooks/commit-msg: No such file or directory"?
This error occurs when Git tries to execute a commit-msg hook that doesn't exist or has an invalid configuration. The hook file may be missing, have incorrect permissions, contain an invalid shebang line, or reference a non-existent interpreter. The fix involves verifying the hook file exists and is properly configured.
What causes "error: cannot run .git/hooks/commit-msg: No such file or directory"?
Husky or another Git hook manager was uninstalled but left configuration behind The hook file has a shebang line referencing a non-existent interpreter (e.g., #!/usr/bin/python when Python isn't installed) The hook file is missing from the .git/hooks or .husky directory
Is the "error: cannot run .git/hooks/commit-msg: No such file or directory" error serious?
It is a low-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading