Azure DevOps上使用git push --force遇到的问题

Azure DevOps上使用git管理代码,发现上次push到远程的代码有点小问题,本地amend修改好上次提交以后,想把远程的提交覆盖掉,Force push发现不能生效,报错如下

$ git push -f
Total 0 (delta 0), reused 0 (delta 0)
To ssh.dev.azure.com:v3/orgName/projectName/repoName
 ! [remote rejected] feature/branch -> feature/branch (TF401027: You need the Git 'ForcePush' permission to perform this action. Details: identity 'XXXX@xxx.xxx', scope 'branch'.)
error: failed to push some refs to 'git@ssh.dev.azure.com:v3/orgName/projectName/repoName'

定位到Branches页面,打开相应branch的Branch security设置,找到自己所在的用户组,比如说Contributor,设置允许Force push

搜索框输入自己的账号,看下最终评估结果是不是允许Force push(有时候可能弄错自己所在的用户组,有时候会因为用户组设置相互覆盖而不生效),这里只是用来确认自己设置的允许Force push确实生效了。

※如果直接搜索自己的账号去设置,有些情况下可能并不生效(用户组设置相互覆盖),完成后再次搜索,可能会像如下一样,发现状态并不正确,push会报如上一样的错误

修改完再次push发现还是不行(注意到branch上有policy徽标)

$ git push -f
Total 0 (delta 0), reused 0 (delta 0)
To ssh.dev.azure.com:v3/orgName/projectName/repoName
 ! [remote rejected] feature/branch -> feature/branch (TF402455: Pushes to this branch are not permitted; you must use a pull request to update this branch.)
error: failed to push some refs to 'git@ssh.dev.azure.com:v3/orgName/projectName/repoName'

再次定位到Branches页面,打开相应branch的Branch Policies设置,发现了以下policy

关闭勾选的项,再次push,终于可以了

$ git push -f
Total 0 (delta 0), reused 0 (delta 0)
To ssh.dev.azure.com:v3/orgName/projectName/repoName
 + 5a3b351...c8fa643 feature/branch -> feature/branch (forced update)
原文地址:https://www.cnblogs.com/makesense/p/14672605.html