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

GitBEGINNERMEDIUMUpdated Dec 6, 2025ยท 136 views

This error occurs when Git cannot execute a hook script, typically due to a missing or invalid shebang line, Windows line endings (CRLF), or a corrupted hook file. Fixing the shebang or regenerating the hook resolves most cases.

Quick fix

Fixes error: cannot run .git/hooks/pre-push: No such file or directory

ls -la .git/hooks/pre-push
  1. Check if the hook file exists and is executable
  2. Inspect the shebang line
  3. Fix or add the shebang line
  4. Convert Windows line endings to Unix
git โ€” error
ls -la .git/hooks/pre-push
error: cannot run .git/hooks/pre-push: No such file or directory

What this error means

When Git shows `error: cannot run .git/hooks/<hook-name>: No such file or directory`, it means Git found a hook file but cannot execute it. Despite the misleading message, the file usually exists - the issue is that the system cannot locate the interpreter specified in the script's shebang line. The most common cause is a malformed or missing shebang line at the top of the hook script. The shebang (e.g., `#!/bin/bash` or `#!/bin/sh`) tells the operating system which interpreter to use. If this line is missing, corrupted, has an empty line before it, or references an interpreter that does not exist on your system, you will see this error. On Windows systems using Git Bash or WSL, this error frequently occurs due to CRLF line endings or file encoding issues (UTF-8 with BOM). The carriage return character makes the interpreter path invalid, causing the same 'No such file or directory' message.

Typical symptoms

  • Git push, commit, or other operations fail with 'cannot run .git/hooks/<hook-name>: No such file or directory'
  • Error appears even though the hook file visibly exists in .git/hooks/
  • Hook worked previously but stopped working after editing on Windows
  • Error started after installing Git LFS or a pre-commit framework
  • CI/CD pipeline fails at git operations with hook-related errors

Common causes

  1. Missing shebang line at the start of the hook script
  2. Empty line or whitespace before the shebang
  3. Shebang references an interpreter that does not exist (e.g., /bin/bash on Alpine)
  4. Windows line endings (CRLF) corrupting the shebang interpreter path
  5. Hook file saved with UTF-8 BOM encoding
  6. Corrupted hook file from Git LFS or pre-commit framework installation
  7. Path contains spaces (common on Windows with SourceTree or other GUI clients)

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

1Check if the hook file exists and is executable

First, verify the hook file exists and has execute permissions:

bash
ls -la .git/hooks/pre-push

If the file exists but is not executable, add execute permission:

bash
chmod +x .git/hooks/pre-push

Replace pre-push with the actual hook name mentioned in your error (e.g., pre-commit, post-receive).

2Inspect the shebang line

Check the first line of the hook script:

bash
head -1 .git/hooks/pre-push

It should show something like #!/bin/bash or #!/bin/sh. If it shows nothing, has garbage characters, or starts with an empty line, that is the problem.

You can also check for hidden characters:

bash
cat -A .git/hooks/pre-push | head -3

Look for ^M (carriage return) at the end of lines, which indicates Windows line endings.

3Fix or add the shebang line

Open the hook file in a text editor and ensure the very first line (no empty lines before it) contains a valid shebang:

bash
#!/bin/sh
# Rest of your hook script

Or if the script requires bash features:

bash
#!/bin/bash
# Rest of your hook script

For better portability across systems, use #!/usr/bin/env sh or #!/usr/bin/env bash:

bash
#!/usr/bin/env bash
# This works even when bash is not in /bin/
4Convert Windows line endings to Unix

If you edited the hook on Windows or see ^M characters, convert to Unix line endings:

bash
# Using dos2unix
dos2unix .git/hooks/pre-push

# Using sed (if dos2unix not available)
sed -i 's/\r$//' .git/hooks/pre-push

On macOS, the sed command is slightly different:

bash
sed -i '' 's/\r$//' .git/hooks/pre-push
5Check file encoding for UTF-8 BOM

Some editors save files with a UTF-8 Byte Order Mark (BOM), which breaks the shebang:

bash
file .git/hooks/pre-push

If it shows 'UTF-8 Unicode (with BOM)', re-save the file as plain UTF-8 without BOM.

In VS Code: Open the file, click the encoding in the bottom-right (e.g., 'UTF-8 with BOM'), select 'Save with Encoding', and choose 'UTF-8'.

6Verify the interpreter exists on your system

Ensure the interpreter specified in the shebang actually exists:

bash
# Check for bash
which bash

# Check for sh
which sh

If you are using Alpine Linux or a minimal Docker image, /bin/bash may not exist. Use /bin/sh instead:

bash
#!/bin/sh

Or install bash:

bash
apk add bash
7Regenerate corrupted Git LFS hooks

If you use Git LFS and the hooks became corrupted, regenerate them:

bash
git lfs update --force

This overwrites the existing hooks with fresh copies. If you had custom hooks alongside LFS hooks, you may need to restore them afterward.

8Delete and recreate the hook (if using SourceTree or GUI)

If you use SourceTree or another Git GUI that manages hooks, try deleting the problematic hook and letting the application regenerate it:

bash
rm .git/hooks/pre-push

Then restart SourceTree. It will recreate the hook file on next operation.

Alternatively, create a fresh hook from the sample:

bash
cp .git/hooks/pre-push.sample .git/hooks/pre-push
chmod +x .git/hooks/pre-push
9Bypass the hook temporarily (if needed)

If you need to push urgently while investigating, you can bypass client-side hooks:

bash
# Skip pre-push hook
git push --no-verify

# Skip pre-commit hook
git commit --no-verify -m "Your message"

Warning: This bypasses the hook entirely. Use this only as a temporary workaround, not a permanent solution.

Advanced notes

Why 'No such file or directory' is misleading: When the kernel parses a shebang line like #!/bin/bash\r (with a carriage return), it looks for an interpreter literally named /bin/bash\r. Since that file does not exist, you get 'No such file or directory' - even though both /bin/bash and the hook file itself exist.

Pre-commit framework hooks: If you use the pre-commit framework (https://pre-commit.com/), hooks are managed by the tool. Run pre-commit install --overwrite to regenerate them. The framework uses Python, so ensure your shebang points to a valid Python interpreter.

Windows WSL considerations: When using Git in WSL (Windows Subsystem for Linux), hooks created in Windows may have the wrong line endings. Always edit hooks from within WSL using a Linux editor, or configure your Windows editor to use LF line endings for files in WSL paths.

Shared hooks with core.hooksPath: If your team uses shared hooks via git config core.hooksPath, ensure the shared directory is accessible and scripts have correct permissions. The path must be absolute or relative to the repository root.

Server-side hooks: For pre-receive, post-receive, and other server-side hooks on remote repositories (like Bitbucket Server), the same principles apply. Check the hook files on the server, not your local machine.

Frequently asked questions

What does "error: cannot run .git/hooks/pre-push: No such file or directory" mean?
git/hooks/<hook-name>: No such file or directory, it means Git found a hook file but cannot execute it. Despite the misleading message, the file usually exists - the issue is that the system cannot locate the interpreter specified in the script's shebang line. The most common cause is a malformed or missing shebang line at the top of the hook script.
How do I fix "error: cannot run .git/hooks/pre-push: No such file or directory"?
This error occurs when Git cannot execute a hook script, typically due to a missing or invalid shebang line, Windows line endings (CRLF), or a corrupted hook file. Fixing the shebang or regenerating the hook resolves most cases.
What causes "error: cannot run .git/hooks/pre-push: No such file or directory"?
Missing shebang line at the start of the hook script Empty line or whitespace before the shebang Shebang references an interpreter that does not exist (e.g., /bin/bash on Alpine)
Is the "error: cannot run .git/hooks/pre-push: No such file or directory" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading