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下選擇允許那些角色或具體那些用戶可以提交,在這里可以選擇你自己
設置完畢后再重新提交就成功了。