paul
paul

Reputation: 4487

Error on `git push` to Bitbucket is `! [remote rejected] master -> master (pre-receive hook declined)`

My git push to my remote Bitbucket server failed with:

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

I think it's a different issue than this (remote rejected master -> master (pre-receive hook declined)), and many people are facing the same issue, so I am posting a new question here.

My friend added me on bitbucket.org as an admin so that I can help on his project. So, I wanted to start by pulling the latest code to my local computer.

Steps I followed:

  1. Go to empty folder
  2. git init
  3. git remote -v (this gives nothing)
  4. git remote add origin <path to git repo>
  5. git remote add master <path to git repo>
  6. git remote -v (this show both with fetch and push in brackets)
  7. git fetch origin master
  8. git pull origin master (I have latest code now, all files and folders)
  9. touch test (lets test a "test" commit)
  10. git status
  11. git add .
  12. git commit -m "testing, first commit"
  13. git push origin master

error:

Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 274 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: permission denied to update branch master
To '<repo path>'
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to '<repo path>'

Upvotes: 56

Views: 170961

Answers (9)

Gabriel Staples
Gabriel Staples

Reputation: 52777

Temporary, "mutex-style" fixing of "Branch restrictions" in Bitbucket Cloud to allow one-time branch creation of branches with protected names

Quick summary

  1. Temporarily change branch restrictions from Write access None to Everybody.
  2. Do your git push to push a new branch with a normally-protected name to the remote git server.
    # Example
    git push -u origin release/v1.2.3 
    
  3. Change branch restrictions back to None after you're done, to prevent people from pushing commits directly to these branches with git push. This ensures they must go through the PR (Pull Request) process.

Details

If git push to a remote Bitbucket Cloud git server fails with the following error message:

! [remote rejected] release/v1.2.3 -> release/v1.2.3 (pre-receive hook declined)

...then it may be due to permissions issues.

Of the big 3 git hosting services (GitHub, GitLab, Bitbucket), Bitbucket Cloud seems to be the least developed, so you may have a conundrum:

You want to protect your main and release/* branches from direct pushes, but doing so also accidentally blocks you from creating new release/* branches :(.

You go to https://bitbucket.org/mycompany/workspace/projects/myproject/settings/branch-restrictions

...and protect the main and release/* branches as follows, with Write access set to None:

enter image description here

This is good! It prevents people from pushing to these branches so they can't skip the PR (Pull Request) process. Now, the only way to get code into these branches is to create a PR and have it reviewed.

However, the conundrum is that this also blocks you from creating new release/* branches, such as release/v1.2.3. You can't push them to the remote server because of the permissions issue, and you get the error above.

While other git hosting services have this solved, Bitbucket Cloud does not yet.

So, to push a new release branch under a normally-protected branch name, do the following:

  1. Go to your project or repo branch restrictions settings. Ex: https://bitbucket.org/mycompany/workspace/projects/myproject/settings/branch-restrictions

  2. Click the Edit link in the image above, for release branches.

  3. You'll see this:

    enter image description here

  4. Uncheck the option which says Only specific people or groups have write access, and change it to Everyone with access to the repository has write access instead.

  5. Click Save.

  6. Now you can push your new release branch to the remote server.

    git push -u origin release/v1.2.3 
    
  7. Now go back to the branch restrictions settings and change it back to Only specific people or groups have write access.

  8. Done.

In this way, you are using the branch permissions set online as a sort of write protection "mutex" to prevent people from pushing directly to these branches, but you can temporarily disable this write protection when you need to create and push a new release/* branch, such as release/v1.2.3.

Note

In Bitbucket Cloud, there should be, but currently isn't, an option to allow creating new branches even if Write access is set to None to block pushing new commits directly via git push. I hope Bitbucket adds this feature in the future. I believe GitHub and others already have it.

Upvotes: 0

coffman21
coffman21

Reputation: 1063

I wanted to force push on my ticket branch, and it was rejected. Went to Repository settings -> Hooks -> found out that Reject Force Push was enabled, and disabled it.
Use at your own risk.

Upvotes: 0

Uchiha Suryajit
Uchiha Suryajit

Reputation: 147

Another scenario -> I also had this issue and I also configured global parameters, tried branching etc but it didnt work for me.

Below was the issue -> I recently joined my current firm and till my Bitbucket credentials got activated, I was using someone elses credentials to access Jira, Bitbucket etc. Now this credetials got saved in Windows inside "Credential Manager". After deleting those other colleague credentials, it worked fine for me.

Upvotes: 0

Kamil Kiełczewski
Kamil Kiełczewski

Reputation: 92467

I have a better solution on Bitbucket: If you are admin go to Repo >Settings >Limit Pushes section, and in "Branch name" put 'master', and in "..search... user" put your user name - then push "ADD" - and you are done :)

Upvotes: 2

First Zero
First Zero

Reputation: 22356

It looks like the branch management (one of the admin settings) in bitbucket has been configured to only allow certain people to push directly to master.

Try creating a branch - git checkout -b test, create your test commit and push git push origin test:test. You can always cleanly delete this branch once you have completed the test.

The other option (probably best agreeing with whoever set this up) is you go to bitbucket and head to admin, branch management and remove master be too limited. cf - https://confluence.atlassian.com/display/BITBUCKET/Branch+management

Upvotes: 61

Abdul Manan
Abdul Manan

Reputation: 2375

The same issue with me on gitlab, I asked the repo owner to grand me maintainer role and the issue fixed.

Upvotes: 0

Euripides Georgantzos
Euripides Georgantzos

Reputation: 636

Just reporting another case leading to the specified error. If the Bitbucket repository size reaches 2 GB, Bitbucket itself restricts the repository to read-only access. When that happens, contributors with read/write permissions are not able to push and get the "pre-receive hook declined" error.

I also found these documents from Atlassian: https://blog.bitbucket.org/2014/05/30/repository-size-limits/ https://confluence.atlassian.com/bitbucket/reduce-repository-size-321848262.html

Links include a guide on how to restore the repo back to full functionality

Upvotes: 5

Shina
Shina

Reputation: 2066

What worked for me was:

  1. Created a new branch locally
  2. Bitbucket -> Settings -> Repository details = changed default branch (e.g to prod)
  3. Bitbucket -> Branches -> delete master branch (you can do this without step 2)
  4. Then push your locally created branch (e.g master)
  5. Bitbucket -> Settings -> Repository details = changed default branch (e.g to master)

Upvotes: -2

Savad KP
Savad KP

Reputation: 1654

I think package setuptools/distribute is listed in requirements.txt. Please remove the same.

Upvotes: 1

Related Questions