git撤销远已经push到程服务器上某次提交

git撤销远已经push到程服务器上某次提交

问题: 不小心把一次错误的代码push到远程服务器上的分支上,或者没有merge强制将本地的方法push到git服务器上。

解决方法:

输入 git log。 找到错误提交之前的git push的 commit_id (即正确时候的commit_id)

git log

输入 git reset --hard <commit_id> 和 git push origin HEAD --force
 
git reset --hard f358e732.... (或者保留本地修改 git reset --soft f358e732....)

git push origin HEAD --force
 

看git历史,之前错误的那个push已经没有了。

 ------------------------------------------------------------------------------

当我们有时候回滚了代码,想强制push到远程仓库的时候,

git push origin --force

会报如下错误:

You are not allowed to force push code to a protected branch on this project

如果用的是gitlab版本库,这说明gitlab对仓库启用了保护,需要在仓库中设置一下:

"Settings" -> "Repository" -> scroll down to "Protected branches".

1.可以直接点该分支旁的Unprotect按钮,解除保护,但是这种方法不推荐

2.第二种方法是在Allowed to push下选择允许那些角色或具体那些用户可以提交,在这里可以选择你自己

设置完毕后再重新提交就成功了。

原文地址:https://www.cnblogs.com/wgscd/p/13884336.html