【Git】push时error: failed to push some refs to ‘xxx.git’

First look at the screenshot of the error report:

I submitted the code on the 20220705 branch, and when I pushed to the remote, the error shown in the figure above occurred:

! [remote rejected]     20220705 -> 20220705 (pre-receive hook declined)
error: failed to push some refs to 'xxx.git' 

In fact, the reason for the error is in the picture: You are not allowed to push code to protected branches on this project.

The branch permission of the current branch is protected, that is, the protected branch does not allow us to push the code to the branch.

There are two solutions:

1. Create a new branch push code, then merge

// 新建分支步骤
// 1、切换分支--从20220705分支新拉一个分支,所以这里我切换到了该分支
git checkout 20220705

// 2、新建分支并且切换到该分支
git checkout -b newBranch

// 3、推送到远程仓库
git push origin newBranch

// 4、关联分支
git branch --set-upstream-to=origin/newBranch newBranch
// 或者
git push --set-upstream origin newBranch

// ====================分割线=================

//合并代码(假设newBranch分支上已经push了需要提交的代码)
// 1、切换分支
git checkout 20220705

// 2、合并代码
git merge origin/newBranch

2. Modify branch permissions (not recommended)

Generally speaking, if you are allowed to push code directly on the branch, you should not set the branch permission to be protected. It is better to create your own branch honestly, don't be lazy like me!

Guess you like

Origin blog.csdn.net/weixin_38629529/article/details/125566070