344

I have run gitlabhq rails server on virtual machine, following 1-6 steps from this tutorial https://github.com/gitlabhq/gitlab-recipes/blob/master/install/centos/README.md and starts rails server executing command sudo -u git -H bundle exec rails s -e production. After that I created user, using admin tools and created new project under this user. Then I'm trying to push the existing project to this repo as always. But in the last step, git push origin master fails with the error

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

Additional info:

1) I haven't activated user (project owner) via email activation link, because I haven't configured post service on server-side and I didn't find instructions how to do that in this manual.

2) Gitlab server generates tips how to push project to repo and there is not repositories/ in path. I mean it generates git@mygitlabhost:user/repo.git instead of git@mygitlabhost:repositories/user/repo.git which is correct.

3) When i tried to debug it, I opened pre-receive script inside repo on server and tried to output variables (there is 3 of them): refs = ARGF.read, key_id = ENV['GL_ID'] and repo_path = Dir.pwd and found, that key_id is always empty. Maybe the problem is here... If so, please give me suggestions on how to fix that. Thanks

5
  • 104
    I swear, there is at least 1 person in every software development team that wakes up everyday and think how to make other developers' lives much harder.
    – samayo
    Commented Jun 23, 2018 at 20:44
  • 1
    It looks like there's a very popular answer that must work for most people. In my case, however, the same message was received from GitHub when there were accidentally another push already in progress from another machine when trying to push.
    – Hack-R
    Commented Feb 18, 2019 at 21:14
  • You cannot do anything from your end. Just check with repository admin for the new push policies they have incorporated. It could be because they have implemented policy like commit comment must include something special for example it should include Jira ticket number in comment or similar to this. Commented May 30, 2022 at 8:58
  • I just encountered this issue as well. For me, the resolution was in how my branch was named. The server administrator (our DevOps person) had added a check on the branch name format and I was failing this check. Once I renamed my branch locally then the new branch push worked fine. Commented Aug 10, 2023 at 18:38
  • In my case I got this error because of trying to push large files to github. So I added .angular/ to the .gitignore file. This resolved my issue. Commented Mar 17 at 17:02

29 Answers 29

398

GitLab by default marks master branch as protected (See part Protecting your code in https://about.gitlab.com/2014/11/26/keeping-your-code-protected/ why). If so in your case, then this can help:

Open your project > Settings > Repository and go to "Protected branches", find "master" branch into the list and click "Unprotect" and try again.

via https://gitlab.com/gitlab-com/support-forum/issues/40

For version 8.11 and above how-to here: https://docs.gitlab.com/ee/user/project/protected_branches.html#restricting-push-and-merge-access-to-certain-users

12
  • 17
    UI has changed a bit: After project "Settings", click "Repository" tab. Then "Protected Branches"
    – Donn Lee
    Commented Jul 6, 2017 at 21:11
  • So I was having the same problem OP had and this solution worked for me, thanks! But now I'm wondering, why? I've been using gitlab for a while and all of a sudden I have do this? What changed? Commented Oct 19, 2017 at 13:33
  • 2
    Since GitLab 8.11 the options has changed a bit, check the details here
    – Arghya C
    Commented Mar 29, 2018 at 9:34
  • facing same issue. Here I'm not even able to push other branch as well. I'm pushing it first time though. Commented Aug 13, 2019 at 11:52
  • 1
    i removed unprotected but what will happen now ? anyone else in the world can work on this repo ?
    – Flutterer
    Commented Oct 15, 2019 at 7:59
72

In my case, my team lead to created a repo(repo was empty) and assign me as developer so when I pushed to the code directly to master the error I was facing ! [remote rejected] master -> master (pre-receive hook declined) So how it was fixed that he assigned to me as maintainer so I was able to push the code directly to the master.

3
  • How do I do that? Commented Jun 14, 2021 at 6:24
  • Ask the repo owner to give you the right to the maintainer Alternative: Ask repo owner to push something/Readme.md in master then you can push the code, if you are still facing the issue you have to push code with -f command, full command would be git push origin master -f.
    – Saad Qamar
    Commented Jun 14, 2021 at 11:28
  • I created my own repo, then I tried to push on the main branch to init (following the initial readme of Gitlab), it didn't work until I unprotected the main branch. Something is sill unclear. Commented Jan 7, 2022 at 8:02
19

Following resolved problem in my local machine:

A. First, ensure that you are using the correct log on details to connect to Bitbucket Server (ie. a username/password/SSH key that belongs to you) 

B. Then, ensure that the name/email address is correctly set in your local Git configuration: Set your local Git configuration for the account that you are trying to push under (the check asserts that you are the person who committed the files) * Note that this is case sensitive, both for name and email address * It is also space sensitive - some company accounts have extra spaces/characters in their name eg. "Contractor/ space space(LDN)".  You must include the same number of spaces in your configuration as on Bitbucket Server.  Check this in Notepad if stuck.

C. If you were using the wrong account, simply switch your account credentials (username/password/SSH key) and try pushing again.

D. Else, if your local configuration incorrect you will need to amend it

For MAC

open -a TextEdit.app ~/.gitconfig

NOTE: You will have to fix up the old commits that you were trying to push.

  1. Amend your last commit:

    > git commit --amend --reset-author
    
      
    <save and quit the commit file text editor that opens, if Vim then
    :wq to save and quit>
    
  2. Try re-pushing your commits:

    > git push
    
14

Seems the problem is with some services, like sidekiq. Running sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production outputs all the problems with config.

2
  • I had the problem with GitLab, just had to restart.
    – secavfr
    Commented Jan 2, 2022 at 1:04
  • In my case I got this error because of trying to push large files to github. So I added .angular/ to the .gitignore file. This resolved my issue. Commented Mar 17 at 17:02
11

This is because Master branch is protected, Here are the steps to clear this error.

  1. Create a branch in your master branch git checkout -b new-branch-name
  2. Push the new branch git push --set-upstream origin new-branch-name
  3. Now remotely merge the branch into master.
  4. Once merged, reset the local master branch git reset --soft HEAD~1
  5. Finally git pull
2
  • 2
    Not the right solution. It'll display the same error as master. Commented Dec 22, 2021 at 12:57
  • This works for me. The only piece is another step between 3&4: git checkout master. Commented Apr 1 at 0:25
10

this worked for me.

In the latest version of GitLab you can find it under:

Your GitLab project > Settings > Repository > Protected Branches One or both of the settings (Allowed to merge / push) set it to: Developers + Maintainers or make the person Maintainer.

https://gitlab.com/gitlab-org/gitlab-recipes/-/issues/27

6

ihave followed the instruction of heroku logs img https://devcenter.heroku.com/articles/buildpacks#detection-failure ( use cmd: heroku logs -> show your error) then do the cmd: "heroku buildpacks:clear". finally, it worked for me!

0
4

Might not be the case, but this was the solution to my "pre-receive hook declined" error:

There are some repositories that only allow modifications through Pull Request. This means that you have to

  1. Create a new branch taking as base the branch that you want to push your changes to.
  2. Commit and push your changes to the new branch.
  3. Open a Pull Request to merge your branch with the original one.
0
4

Go to Project settings --> Hooks --> (Under) Pre-receive hooks

Disable cp require issue reference in commits

3

You need to add your ssh key to your git account,if it throws error then delete previous ssh key and create a new ssh key then add.

5
  • 1
    That didn't help. :( Tested on both accounts - root and myuser on vm and local machine resp.
    – Hroft
    Commented Feb 4, 2015 at 14:03
  • check git remote -v, your git repo is added or not ? Commented Feb 4, 2015 at 17:00
  • 'git remote -v' outputs: origin git@mygitlabhost:repositories/user/repo.git (fetch) origin git@mygitlabhost:repositories/user/repo.git (push)
    – Hroft
    Commented Feb 5, 2015 at 6:42
  • If you're using hosted gitlab, the problem is likely on their side. Just try again a few minutes later. Commented Aug 10, 2018 at 21:21
  • Answers like given here should get an extra negative penalty cause there is no reason whatsoever given WHY this would work or how SSH is related to the problem stated.
    – wh81752
    Commented Jul 17, 2022 at 14:46
3

You might not have developer access to the project or master branch. You need dev access to push new work up.

New work meaning new branches and commits.

3

I stumbled across the same error using BitBucket. I had a local Git repo I wanted to back up online, so I created a new repository from my BitBucket account (using the web interface).

After running git remote add origin [email protected]:StatMarianne/<a private repo>.git, I ran git push origin master to no avail (I typically don't use the -u option with git push because I don't mind typing up the full repo and branch names when pushing and pulling).

The error read:

remote: You're not allowed to write to this repository.
To bitbucket.org:StatMarianne/<a private repo>.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to '[email protected]:StatMarianne/<a private repo>.git'

But my local branch did get successfully pushed when I ran git push -u origin master (following strictly BitBucket's instructions).

I find it surprising, since -u (--set-upstream) should only "add upstream (tracking) reference..." Anyhow.

3

If you are facing issue like

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

its beacuse master is protected, you can do 2 steps

To remove the protection, follow these steps –

  1. Go to project Settings.

  2. Click on Repository tab.

  3. Open Protected Branches.

  4. Find your master branch and click on Unprotect.

Or else

We can switch branches from master to main

  1. git checkout -b main

  2. git branch -D master

  3. git push heroku main

Happy Hacking!!!

2

Despite the question is specific to gitlab, but similar errors can happen on github, depending how it is set up (usually Github Enterprise).

You need to familiarize yourself with the following concepts:

  • pre-receive hooks
  • organization webhooks
  • Webhooks

Webhooks are more commonly understood than other two items.

The Pre-receive hooks

are scripts that run on the GitHub Enterprise server to enforce policy. When a push occurs, each script runs in an isolated environment to determine whether the push is accepted or rejected.

Organization webhooks:

Webhook events are also sent from your repository to your "organization webhooks". more info.

These are set up in your github enterprise. They are specific to the version of the github enterprise. You may have no visibility because of your access limitations (developer, maintainer, admin, etc).

1
  • Why the negative vote? This is an undocumented new behaviour that needs to be out there for community.
    – Sohail Si
    Commented Aug 25, 2020 at 17:09
1

I solved this issue by changing remote 'origin' url from http to git protocol in .git/config

1

I had the same problem while working with Bitbucket.

On push, the error was 'The commit message doesn't match the regex **".

Got to 'Settings' > 'Hooks' > Enable 'Yet another commit checker' option.

And it started working!!

1
  • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review
    – Bracken
    Commented Nov 16, 2021 at 16:33
1

I recently faced this issue on Gitlab and none of the above solutions worked for me.

Finally what I realized was that the public key was deployed on the repository by my colleague who had left the company and his account was disabled a few weeks later. This caused the key deployed by him rendered unusable, even though it wasn't his personal key.

I deleted the key and re-uploaded the same one and it worked. Permanent solution is to create a service account and upload keys using that account.

Quoting from the Gitlab documentation.

All deploy keys are associated to an account. Since the permissions for an account can change, this might lead to scenarios where a deploy key that was working is suddenly unable to push to a protected branch.

0

Please try:

git push -u origin master

If you still aren't able to push successfully, Make a new branch or choose another one and Push on the new Branch

0

I've experienced that just because of AWS breakage.

To exclude this just check if the message contains something similar to

<head><title>504 Gateway Time-out</title></head>

So if you haven't done anything suspicious before and just started to see this - it may be just server timeouts.

0

TL;DR Rename project and copy all the changes over.


I tried almost all answers on this page.

In the end I fixed it by doing this:

  • Renaming my project to project-tmp
  • Cloning the project again to project
  • Manually moving all my changes from project-tmp to project (and not copying over the .git-folder obviously).
  • git add .
  • git commit -m 'Blah blah'
  • git push

That worked!


Why this worked?

My theory is, that somewhere in some commit (or something) there was a bad piece of settings that screwed it up.

Git LFS

Another thing, that I did, was that I setup git lfs. I'm not sure if that was important or not.

0

For me two possible cases:

  1. The branch is protected and you do not have the right to push (see the role of your git user)
  2. Allowed to force push is disabled on this branch and you maybe you are trying to force push.
0

solution:

  • you need to remove .lock file, yarn.lock or package-lock.json file, for safety you can move this file another folder
  • then, git add .
  • git commit -m 'update'
  • git push heroku main
img1 img2
0

for me this happened because of the previous pending commints which were not meeting certain criteria's. after below command it worked for me git reset --hard commitId (commit id of the successful commit before unsucessful ones)

0

I had the same problem going around for a while, in my case I was working in a group and had cloned the repository but created it in a separate repository. Two things happened

1.- The repository was created privately. 2.- The repository had not been assigned the necessary permissions to upload to it remotely.

You can check if you are working just create a new public repository and change the path of the remote one this way you can test it.

in my case as I was working in a group just change my url to the creator repository and that's it, push and it worked perfectly, I hope I can help you.

git remote set-url origin https://github.com// <-- URL

git push -u origin main

0
Only specific people or groups have write access means Branch is restricted.

If you are using Bitbucket follow bellow steps:

  1. Login in your Bitbucket Account.
  2. Select Your Repository
  3. Click On Repository Settings
  4. Click On Branch restrictions
  5. All restricted branch will be list click Edit
  6. You can add your user in Only specific people or groups have write access section enter image description here

7. Click on Save button 8. Again try to push

0

In my case the gitlab service was down when I got this error - this happened on 07-07-2023

enter image description here

0

for me this happened because of current credentials that i was using those not have sufficient permission to push to that specific branch.

  • First option login with the right account credential that have sufficient permission
  • Second option Add role to de current current account (Reporter, Developer, Maintainer)
  • Third option it is update who can push in the specific branch

Hoppe this will be helpful

-1

I've faced the same issue, this is because I am pushing my code directly in the master branch, and I don't have rights for this. So I push my code in a new branch and after that, I created a pull request to merge with master.

-3

Please check if JIRA status in "In Development". For me , it was not , when i changed jira status to "In Development", it worked for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.