Dave
Dave

Reputation: 19160

Git error when trying to push -- pre-receive hook declined

When I try and push a change I've committed, I get the following error:

git.exe push -v --progress  "origin" iteration1:iteration1

remote: *********************************************************************
To ssh://git@mycogit/cit_pplus.git
! [remote rejected] iteration1 -> iteration1 (pre-receive hook declined)
error: failed to push some refs to 'ssh://git@mycogit/cit_pplus.git'

What's going on ?

Upvotes: 447

Views: 1433752

Answers (30)

H.S.
H.S.

Reputation: 12679

I got this error while pushing my code changes:

pre-receive hook declined

As suggested in other posts, I have checked the settings in Bitbucket and they were all good.

When I did some more investigation around it, I found that user.name and user.email was missing in the config -

Command to check config:

git config --list

If the output of above command doesn't show user.name and user.email or show wrong value, follow the below steps (In my case user.name and user.email were missing) -

Command to set user.name:

git config --global user.name "your name"

Command to set user.email:

git config --global user.email "[email protected]"

After setting user.name and user.email, git push was still failing because the commit Author was incorrect -

Command to check commit author:

git log

Check the topmost commit Author in the output log.

In my case, the email in commit author was - username@hostname, which seems something assigned as default by git as user.name and user.email were not configured when I did git commit.

To fix this, I deleted the recent local commit without destroying the work (soft reset) -

Command:

git reset --soft HEAD~1

Run git log to confirm your most recent commit is deleted.

Since the recent local commit is deleted, my changes were showing in staging area. Verify changes to be committed -

Command:

git status

This will show the list of all the files to be commit.

Commit changes:

git commit -m "commit message"

Push them:

git push

If branch is local branch -

git push origin -u "your local branch name"

Setting user.name and user.email is suggested in comments of couple of posts.
But, I think, it's worth to post it here as an answer with all the commands and steps that I followed to fix this problem.

Upvotes: 0

chandan jena
chandan jena

Reputation: 1

the way it fixed for me that .. it was expecting the branch in format that like feature/[somefeaturegroup] . After that it accept it.

Upvotes: 0

Martin
Martin

Reputation: 2591

I got the same response when trying to migrate a git repo into a GitHub Enterprise server I administer. The process I often use for this is (simplified)

git clone --mirror <source>
git push --mirror <dest>

When I did the mirror push I got 1000s of these pre-receive hook declined messages for tags (and other refs), but if I pushed a tag in the error output manually e.g. git push <dest> <tag> then it was accepted.

The issue seems to be because there were so many refs >200k. My best guess is there is a timeout (possibly hard limit) somewhere in git (or GitHub Enterprise) that then produces this hook error a little erroneously IMO.

The solution I came up with was to push all the refs separately first. My initial, naive approach of using xargs to read each ref and push it, is painfully slow. I found this much faster method that also does not hammer the server.

# Get all the refs into a file
git show-ref | awk '{ print $2 }' > all-refs.txt

# Split the file into manageable chunks
# I found 500 was okay, but 1000 gives "[remote rejected] ... (failure)" errors
split -a 4 -l 500 all-refs.txt

# Process all these files - you can run this in parallel to further reduce the time
for file in x*; do time git push <dest> $(cat $file); done

# Now when you do the final sync the refs already exist in the destination repo and don't error
git push --mirror <dest>

This is still not fast, just much faster and less load on the server. I ran this on the destination GHE server (so there was no network bottle neck) and my 24cpu 256Gb server was still taking 4.5 mins to process each of the files (500 refs).

Caution: When I did this import there were runners enabled by default on the repo, and the repo had GitHub actions in it that triggered on commit. The effect of pushing the refs this way triggered the action, which then queued 1000s of jobs. This in turn stopped other actions running on the server! Make sure to disable actions (and maybe other hooks) on the repo first.

Upvotes: 1

Jeevan Shinde
Jeevan Shinde

Reputation: 1

Open the repository settings on you respective repository. Then, as per the screenshot shown below, in "Repository Details", go to "ADVANCED" options. There you can see "File size limits", if it's checked then uncheck it. Then try again to push to the repository.

Bitbucket Repo Setting

It's just size issue.

Upvotes: 0

Md Saif
Md Saif

Reputation: 1

login to your GitHub account go to repository and click on your repo "cit_pplus" click on settings ==> scroll down and disable branch protection rules

it will fix this issue

Upvotes: -1

Wovasteen Gova
Wovasteen Gova

Reputation: 58

Tried most of the suggestion here. Didn't work (Github)

Just delete the .git file

rm -rf .git

reinitialize a new rebo

make sure to use

git push -u --force origin master

as the last step

Follow steps here: I know its destructive but iv tried everything else

Upvotes: -5

Gabriel Cecon Carlsen
Gabriel Cecon Carlsen

Reputation: 419

i had two different lock files in my app, deleting the unused one solved.

Upvotes: 0

ozkary
ozkary

Reputation: 2694

File size is important. There is a limit of ~120MB for a single file. In my case, .gitignore using Visual Studio had the file listed, but the file was still committed. When using the git cli, we can get more detail information about the error.

pre-receive hook declined was as a result of the big file. Basically validating the push.

To resolve it, I removed the last commit using:

git reset --soft HEAD~1

I then excluded the file from the commit.

Note: Use HEAD~N to go back to N number of previous commits. (i.e. 3, 4) Always use the --soft switch to maintain changes in the folder

Upvotes: 127

poza
poza

Reputation: 1

In my case, it was jira issue integration under project settings in BITBUCKET. By means, we need to put jira issue associated with it. I had two options 1) disable jira issue in the commit 2) put jira issue reference to the commit.

Upvotes: 0

Sumit Bajaj
Sumit Bajaj

Reputation: 39

Step1:

git pull --rebase

Step2:

git reset --soft HEAD~1

Step3:

git commit -m "your comments here"

Step4:

git push

This should fix your issue.

Upvotes: 3

001001
001001

Reputation: 588

My issue was file size, trying to migrate a project from gitlab to github, and the error did hint at it for me, directing to this page https://docs.github.com/en/repositories/working-with-files/managing-large-files/moving-a-file-in-your-repository-to-git-large-file-storage

The command used was:

git lfs migrate import --everything --above=100kb

After this I was able to git push --mirror http:...

Upvotes: 4

Tom Betthauser
Tom Betthauser

Reputation: 1

In my case I manage the project myself but recently upgraded it to the hobbyist tier. In any case checked the logs through herokus browser interface and it had a very clear message about the node version (its an express app) not being defined in the package.json. They had a link to a heroku page outlining how to fix it and it worked.

In any case I just added this to my package.json and no further issues!

  "engines": {
    "node": "16.x",
    "npm": "6.x"
  },

I checked my node version and npm versions first though just to be clear. My node version was 16 but my npm version was 7. I decided to leave the npm defined as 6.x just because it's what was listed on the heroku page. Didn't encounter any issues.

$ node --version

$ npm --version

And here's the link to the heroku help page for this.

https://devcenter.heroku.com/articles/nodejs-support#specifying-a-node-js-version

Upvotes: 0

Dileep Amrani
Dileep Amrani

Reputation: 1

! [remote rejected] master -> master (pre-receive hook declined)

I spend 2 days on this bug! After a lot of research, I finally found a solution for this.

  1. Heroku config:set USE_NPM_INSTALL=false
  2. Configured the yarn build pack on Heroku
  3. Make sure yarn.lock file is NOT in your .gitignore file

It will work!

Upvotes: -1

Kumar Pankaj Dubey
Kumar Pankaj Dubey

Reputation: 2307

Simple and Quick fix:-

git commit -m "branch_name:added git script"

Upvotes: -1

gutte
gutte

Reputation: 1473

On my case (Bitbucket), the problem was Rewriting branch history is not allowed restriction. Go to Repository settings -> Branch Permissions edit the selected branch's permissions, and check Allow rewriting branch history

Allow rewriting branch history

Upvotes: 28

vijay9908
vijay9908

Reputation: 57

If in a corporate gitlab/ github and you trying to push changes to a branch/master, Pre-hook could mean that you missed some parameters input into the commit message.

In my case, along with message i was needed to add bug id, bug url and approver in commit file and then push to branch and place the merge request.

Hope this helps, as when writing commit message ask your mentor/buddy for th e requirements in a commit message.

Upvotes: 0

themenace
themenace

Reputation: 2800

I got the same error ! [remote rejected] ... (pre-receive hook declined) today.

It was caused by Bitbucket having a problem. So make sure that your Git Remote is up and running.

The issue that prevented me from pushing was: https://bitbucket.status.atlassian.com/incidents/r8kyb5w606g5

Upvotes: -1

Vitalina
Vitalina

Reputation: 51

I resolved this issue with regenerating SSH key and adding it to the GitHub account.

Upvotes: 1

deponovo
deponovo

Reputation: 1432

This is a rather old question and has many answers already. But I would like to share the solution that worked for me while working with bitbucket (no admin rights). Most of my colleagues (to the exception of one) did not face any issues. The both of us could suddenly not push to precise branches any longer (I could not figure out the actual reason). The solution was:

  1. delete existing pull requests related to the problematic branches,
  2. delete the branches on server side,
  3. re-push from local to remote repo, and,
  4. create the pull request again.

Upvotes: 0

Anurag Jain
Anurag Jain

Reputation: 21

In my case there was a Committer restriction in a project on Gitlab:

Users can only push commits to this repository that were committed with one of their own verified emails.

Since I also configured SSH on my machine, my global_user_email was updated in the git config file with my machine's address, hence the remote was not allowing to push.

You can find this at:

  • Gitlab -> Settings -> Repository -> Push Rules

Just disable the commit restriction and it will work.

Upvotes: 1

Nhat Cap Dang
Nhat Cap Dang

Reputation: 1

Your commit is not compatible with the repository maintainer's rules, you just need to git reset --hard HEAD ~ 1 in order to delete the last commit. After that commitment according to the maintainer's rules is ok

Upvotes: 0

Daman Jha
Daman Jha

Reputation: 1

If you facing an issue related to pre-receive hook declined in git while doing Push. You may have the below reasons:

  1. Maybe your DB backup in your project path app_data is exceeded the 100.00 MB limit of Github.
  2. Check the size of your file if you using it in your project not exceeded the size limit of 10.00MB or any file.

You can resolve this issue by the below steps:

  1. Just do the zip those files and push again git push -u origin develop

Upvotes: -2

JoshuaRDBrown
JoshuaRDBrown

Reputation: 351

In my case I got this error because a branch with the same name already existed. Deleting this branch off of the git server will fix this.

Upvotes: 1

Miles
Miles

Reputation: 149

You should look at the logs. I just ran into the same error and realized from the logs it was because I had a yarn.lock and package-lock.json

Upvotes: -1

Zeus
Zeus

Reputation: 6566

I had permissions issue, after given the right permissions i was able to push the contents. I was pushing a existing project into a new git repo.

Upvotes: -1

TundraGreen
TundraGreen

Reputation: 71

I got this message while trying to delete a remote branch (git push origin --delete [branch-name]). The problem was that the branch was marked un-deletable in bitbucket.

Upvotes: 0

hai jiang
hai jiang

Reputation: 259

in sometimes, because the branch you are pushing has been protected, so you can ask the repository's maintainers to change the protecting status. in git-lab , you can find it in

Settings > Repository > Protected Branches .

:)

Upvotes: 24

Jas
Jas

Reputation: 834

For me everything was working fine until Bitbucket automatically changed their policy today (April 21, 2020). This happens to align with a new feature recently introduced today called Workspaces, so I suspect it has something to do with that.

Workaround: I (as an Admin) followed the instructions to add the email address to Users in the UI (the email you are using can be found git config --list

enter image description here

Upvotes: -1

eTechman
eTechman

Reputation: 41

Remove the protected branch option or allow additional roles like developers or admins to allow these users experiencing this error to do merges and push.

Upvotes: 4

CoolMind
CoolMind

Reputation: 28809

In my case I had a new repository, pushed a branch ('UCA-46', not 'master'), rebased it, forcely pushed again and got the error. No web-hooks existed. I executed git pull --rebase as @ThiefMaster advised, had to rebase again and was able to push the branch. But that was a strange and difficult way.

Then I saw Git push error pre-receive hook declined. I found that my branch became protected. I removed protection and could forcely push again.

enter image description here

Upvotes: 1

Related Questions