报错信息
git -c diff.mnemonicprefix=false -c core.quotepath=false --no-optional-locks push -v --tags origin main:main
POST git-receive-pack (1099 bytes)
remote: GitLab: You are not allowed to push code to protected branches on this project.
Pushing to xxx.git'
To xxx.git'
! [remote rejected] main -> main (pre-receive hook declined)
error: failed to push some refs to 'xxx.git'
解决方案
1、使用--soft 参数 - 只撤销 commit ,保留更改内容在工作目录中(推荐)
git reset --soft HEAD^
2、使用 --hard 参数 - 撤销更改,包括工作目录中的更改,即撤销所有的更改
git reset --hard HEAD^
补充
如果你想撤销多个commits,可以指定HEAD后的^数量(^可以用~替代)
1、撤销最后两个commits,但保留更改,即保留git add .
git reset --soft HEAD~2#
2、撤销最后两个commits,并且撤销更改,即撤销 git add . 操作、撤销更改代码
git reset --hard HEAD~2#
注:在执行以上操作之后,如果需要,可以使用git push -f来强制推送到远程仓库,但这可能会影响其他协作者。