How to Fix Git Error: Pre-Receive Hook Declined
Git is a widely used distributed version control system, enabling developers to collaborate on projects efficiently. However, during git push, you might encounter the “pre-receive hook declined” error. This error occurs when a pre-receive hook, a server-side Git hook, rejects the push due to enforced repository policies.
This article will cover what pre-receive hooks are, why this error occurs, and how to resolve it using various troubleshooting methods. We will also address common causes and solutions for GitHub, GitLab, and Bitbucket repositories.
What is a Pre-Receive Hook in Git?
Pre-receive hooks are server-side Git hooks that execute scripts before a push is accepted. They enforce repository policies such as:
- Code quality checks: Running automated tests before accepting a push.
- Branch protection policies: Restricting pushes to
mainormasterwithout approval. - Commit message validation: Enforcing a specific commit message format.
- Security policies: Preventing the push of large or sensitive files.
When a push violates these policies, Git returns the “pre-receive hook declined” error, preventing the push.
Common Causes of Pre-Receive Hook Declined Error
The error message usually includes a hint about why the push was rejected. Some common reasons include:
- Commit message format violation
- Some repositories require commit messages to follow a specific format, such as including a JIRA ticket number.
- Branch protection policies
- The repository may not allow direct pushes to protected branches (
main,master,develop).
- The repository may not allow direct pushes to protected branches (
- Code quality checks failing
- The pre-receive hook might run linters, tests, or static code analysis, and your code must pass before pushing.
- Repository size limits
- Some repositories limit the size of individual commits or the entire repository.
- Large file restrictions
- Pushing large binary files might be blocked if Git LFS is not set up.
- Force push restrictions
- If force pushes are disabled, trying
git push --forcemay trigger this error.
- If force pushes are disabled, trying
- Pre-receive hook misconfiguration
- The repository administrator may have recently modified the hooks, leading to unexpected failures.
How to Fix “Pre-Receive Hook Declined” Error in Git
1. Read the Error Message
Run:
git push
The output may include an explanation like:
remote: error: commit message does not match format: [JIRA-123] Description
remote: error: pre-receive hook declined
In this case, you must amend your commit message.
2. Fix Commit Message Format
If the repository requires a specific commit message format, you can amend the latest commit:
git commit --amend -m "[JIRA-123] Fixed login issue"
git push --force
git revert HEAD
git commit -m "[JIRA-123] Fixed login issue"
git push
3. Push to an Allowed Branch
If pushing to main or master is restricted, create a new feature branch:
git checkout -b feature-branch
git push --set-upstream origin feature-branch
You can then create a pull request for merging.
4. Run Tests Locally
If the pre-receive hook enforces tests or linters, run them locally:
npm test # For JavaScript/Node.js projects
pytest # For Python projects
mvn test # For Java projects
Fix any failing tests before retrying the push.
5. Check for Large Files
If the repository restricts large files, list large files in your history:
git rev-list --objects --all | sort -k2 -n | tail -n 10
If a large file is detected, remove it:
git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch large-file.zip' \
--prune-empty --tag-name-filter cat -- --all
git push origin --force --all
Alternatively, use Git LFS for handling large files:
git lfs install
git lfs track "*.zip"
git add .gitattributes
git commit -m "Enable Git LFS"
git push
6. Resolve Bitbucket Pre-Receive Hook Declined
In Bitbucket, the error might be caused by:
- Branch restrictions: Check Repository settings > Branch permissions.
- Commit message policies: Check Repository settings > Hooks.
- Force push disabled: Ensure you are not using
git push --force.
If restricted, push to a new branch:
git checkout -b fix-branch
git push origin fix-branch
7. Resolve GitLab Pre-Receive Hook Declined
For GitLab, the error often occurs due to:
- Protected branches: Check Settings > Repository > Protected Branches.
- Commit restrictions: Ensure commit messages meet format requirements.
To check protected branches:
git branch -r
If main is protected, create a new branch:
git checkout -b feature-branch
git push origin feature-branch
8. Resolve GitHub Pre-Receive Hook Declined
On GitHub, check:
- Branch protection rules under Settings > Branches.
- Pre-receive hook policies under GitHub Enterprise.
If hooks enforce status checks, ensure you run:
npm run lint # Example for JavaScript projects
pytest # Example for Python projects
9. Contact the Repository Administrator
If you cannot resolve the issue, reach out to the repository administrator. They might provide:
- Hook logs for debugging.
- Updated repository policies.
- Permissions for bypassing restrictions.
To get the latest repository settings:
git fetch --all
git reset --hard origin/main
Example Fix for Common Errors
Error: Large Commit Rejected
remote: error: pre-receive hook declined - commit too large
Fix: Split the commit into smaller parts:
git reset --soft HEAD~1
git add -p
git commit -m "Part 1"
git add -p
git commit -m "Part 2"
git push
Error: Protected Branch
remote: error: pre-receive hook declined - branch is protected
Fix: Push to a new branch:
git checkout -b dev-branch
git push origin dev-branch
Error: Commit Message Format
remote: error: commit message must start with [JIRA-XXX]
Fix: Amend commit message:
git commit --amend -m "[JIRA-123] Fixed bug"
git push --force
Conclusion
The “pre-receive hook declined” error in Git is a server-side restriction that prevents bad commits, enforces security, and maintains repository integrity. The error usually occurs due to: ✅ Commit message format issues
✅ Branch protection policies
✅ Large file size restrictions
✅ Code quality enforcement (tests, linting)
By following the steps outlined in this guide, you can diagnose and fix the issue efficiently. If you’re still stuck, always check the repository documentation or contact the administrator.
References
- Git Documentation – Hooks
- GitHub Protected Branches
- GitLab Pre-Receive Hooks
- Bitbucket Branch Permissions
Learn more about Replace Master Branch with Another Branch in GIT
