GitLab - `pre-receive hook declined`エラーを解決する

GitlabでRepositoryを作成してコードをプッシュしようとしましたが、 pre-receive hook declinedエラーが発生したときにpushが失敗しました。

$ git push -uf origin main
...
remote: GitLab: You are not allowed to force push code to a protected branch on this project.
To https://gitlab.com/xxx/xxxx.git
 ! [remote rejected] main -> main (pre-receive hook declined)
error: failed to push some refs to 'https://gitlab.com/xxx/xxxx.git'

グーグルをしてみると、Protected branchesのポリシーで、Git Repositoryを作成するときにデフォルトでMaintainerにのみプッシュ権限が付与され、Developerには権限が付与されないようです。 Settings で権限のリセットでプッシュされるように問題を解決できます。

回避策

ブランチを作成すると、メインブランチが「Protected branches」に設定され、Developerはプッシュできませんでした。

重要度に応じて、Maintainerが処理したり、Developerが直接できるように権限を設定したりできます。私は単独で使うgitなので、Protected branchを使わないように変更しました。

GitLabのプロジェクトで Settings -> Protected branchesに入ると、次のように権限を変更または無効にすることができます。

gitlab - how to unprotect protected branches

上記のように Unprotected ブランチに変更後、再び push をしてみると成功しました。

$ git push -uf origin main
Enumerating objects: 89, done.
Counting objects: 100% (89/89), done.
Delta compression using up to 8 threads
Compressing objects: 100% (85/85), done.
Writing objects: 100% (89/89), 5.90 MiB | 3.87 MiB/s, done.
Total 89 (delta 23), reused 0 (delta 0), pack-reused 0
To https://gitlab.com/xxx/xxxx.git
 + 57be214...9f1fe50 main -> main (forced update)
Branch 'main' set up to track remote branch 'main' from 'origin'.

References

codechachaCopyright ©2019 codechacha