Getting "pre-receive hook declined" error while trying to move repository from one project to another in Bitbucket Server

Platform notice: Server and Data Center only. This article only applies to Atlassian products on the Server and Data Center platforms.

Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.

*Except Fisheye and Crucible

Summary

While trying to move a repository from one project to another using git push, getting "pre-receive hook declined" error in the Git client logs.

Environment

Bitbucket Server/DC

Diagnosis

The Git client logs will have the below snippet:

remote: You are attempting to update refs that are reserved for Bitbucket's pull request functionality. Bitbucket manages these refs automatically, and they may not be updated by users.
remote: Rejected refs:

The to, from and merge refs are internal Bitbucket refs that should not be manually altered or directly referenced. If the pull requests still exist or those refs are referenced elsewhere, they will exist on the file system. 

From the Git client logs, you'll also see that a pre-receive hook declined the push operation:

 ! [remote rejected]   refs/pull-requests/420/from -> refs/pull-requests/420/from (pre-receive hook declined)
 ! [remote rejected]   refs/pull-requests/420/merge -> refs/pull-requests/420/merge (pre-receive hook declined)
 ! [remote rejected]   20-AGL-17 -> 20-AGL-17 (pre-receive hook declined)
 ! [remote rejected]   20-AGL-19 -> 20-AGL-19 (pre-receive hook declined)
 ! [remote rejected]   20-AGL-21A -> 20-AGL-21A (pre-receive hook declined)

Cause

The issue is that when the repository was cloned from Source, the clone fetched the pull request references as well. When a push to the remote is done in the Destination, the rewritten PR references are pushed as well, which Bitbucket does not allow.

Solution

To resolve this, follow the steps below:

  • Create a new empty repository in the Bitbucket server
  • Either:
    • Close all the pull requests in the repository. All developers should push their changes to Bitbucket and there should be no open PRs
      or
    • Remove all the PR references by using below command:

      git show-ref | cut -d' ' -f2 | grep 'pull-request' | xargs -r -L1 git update-ref -d

  • Follow the steps for rewriting history and cleaning the repo as documented in: How to rewrite commit history.
  • Push the modified repo to the newly created repo.
  • Check if the operation is successful
    • Have the developers use the new repository
 instead of the old one.

 Please note that these procedures involves a history rewrite and can be destructive.

  • If you choose to proceed, please make sure to take a backup of the repo.
  • We also suggest testing the procedures in a lower/staging environment before performing it in production.




Last modified on Jan 22, 2024

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.