223

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

Guildenstern
  • 1,185
  • 1
  • 11
  • 27
Hroft
  • 2,827
  • 2
  • 16
  • 24
  • 26
    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 Jun 23 '18 at 20:44
  • 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 Git*Hub* when there were accidentally another push already in progress from another machine when trying to push. – Hack-R Feb 18 '19 at 21:14

16 Answers16

277

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

Max Vyaznikov
  • 3,075
  • 1
  • 9
  • 10
  • 8
    UI has changed a bit: After project "Settings", click "Repository" tab. Then "Protected Branches" – Donn Lee Jul 06 '17 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? – Andrew Lamarra Oct 19 '17 at 13:33
  • 2
    Since `GitLab 8.11` the options has changed a bit, check the details [here](https://docs.gitlab.com/ee/user/project/protected_branches.html) – Arghya C Mar 29 '18 at 09:34
  • facing same issue. Here I'm not even able to push other branch as well. I'm pushing it first time though. – Santosh Kadam Aug 13 '19 at 11:52
  • 1
    i removed unprotected but what will happen now ? anyone else in the world can work on this repo ? – Flutterer Oct 15 '19 at 07:59
  • Nope. Anyone from people, who have access to your repo can push (and push with "force" flag to replace) into any of your unprotected branches. Replace master by another set of commits, for example, which means a loose a commit history. – Max Vyaznikov Oct 24 '19 at 10:23
26

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.

Saad Qamar
  • 381
  • 5
  • 6
17

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
    
Mrs KOODA
  • 99
  • 10
Maverick09
  • 1,037
  • 1
  • 11
  • 20
10

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.

Hroft
  • 2,827
  • 2
  • 16
  • 24
5

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!

joe-khoa
  • 329
  • 1
  • 4
  • 7
3

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.
Rocío García Luque
  • 2,357
  • 25
  • 31
2

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.

2

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

Disable cp require issue reference in commits

Manoj
  • 21
  • 1
1

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.

Hardik Hardiya
  • 777
  • 5
  • 14
1

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 git@bitbucket.org: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 'git@bitbucket.org: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.

mkcor
  • 469
  • 4
  • 6
1

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
Rifkan Razak
  • 475
  • 1
  • 4
  • 15
0

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

zzxwill
  • 496
  • 2
  • 6
  • 15
0

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).

Sohail Si
  • 1,912
  • 1
  • 16
  • 30
-1

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.

shweta
  • 39
  • 5
-1

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

Hossein Kurd
  • 1,763
  • 2
  • 33
  • 61
-2

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.

ferozpuri
  • 21
  • 7