If you’ve been using Git for a while, you’ve probably come across the “[remote rejected] master -> master (pre-receive hook declined)” or “pre-receive hook declined gitlab author is not a member of team” or “! [remote rejected] iteration1 -> iteration1 (pre-receive hook declined)” error. This can be frustrating, but fortunately it’s easy to fix.
In this blog post, we’ll show you how to fix this error and get your Git workflows back up and running. Stay tuned!
What is Pre-Receive Hook?
Pre-receive hooks are scripts that run on the GitHub Enterprise Server appliance that you can use to implement quality checks.
When you push into Git, the first script will run to Handke the push from client side called as pre-receive. It will take references from stdin, if it exist non zero means push is rejected and if its zero means push is accepted.
When a push occurs in Git, each script(Pre-receive hook) runs in an isolated environment and perform checks on the content that you are pushing into Git. The push will be accepted by the script if the exit status is 0, or rejected by the script if the exit status is non-zero.
What does Pre-Receive Hook Declined Mean?
Pre-Receive Hook Declined means when you are pushing into a branch/ Git either you don’t have permission to push or the branch is protected.
Pre-receive hooks run tests on code pushed to a repository to ensure contributions meet repository or organization policy. If the commit contents pass the tests, the push will be accepted into the repository. If the commit contents do not pass the tests, the push will not be accepted.
How to Fix Pre-Receive Hook Declined Error?
To fix Pre-Receive Hook Declined Error, You should contact to your GitHub Enterprise Server site administrator. He will provide the access to you and you will able to push the content.
Because they allow organization or repository administrators to enable or disable pre-receive hooks.
If you get Pre-Receive Hook Declined Error in Gitlab, you have to remove or you have to ask administrator to remove the protection from branch. Gitlab by default having Protected branches. To push the content first you have to unprotect it.
Steps to remove protection from branch in Gitlab:
- First Go to your project setting.
- After that click on Repository Tab.
- Find your protected branch in which you are pushing content.
- Now find Master branch and unprotect it.
If you using Bitbucket then during pushing the content you may get error “Rewriting branch history is not allowed“. To fix “Rewriting branch history is not allowed” , first go to Branch permission which is available inside Repository settings and edit the permission and check on “Allow rewriting branch history”.
When number of collaborators increase in a repository its a good thing to main version management system to protect from anomalies.
Why Use Pre-Receive Hook?
There are some examples where Pre-receive hook good to use:
- Lock repository or branch if you want to reject push.
- If you want to prevent PR author to merge their changes.
- If you want to prevent sensitive data to add in repository.
- If you want to follow some specific format or pattern to push the content in repository.
Now you have learned how to fix Pre-Receive Hook Declined error. If you get Pre-Receive Hook Declined error then you can check below scenario to check where the issue can be:
- Check you have permission to push to branch or repository.
- Check configuration os correct or not.
- Check your login credential they are correct or not.
- Check do you need to pull the branch/ repository before push.