GitLab试水,解决push error

即将提交毕业论文(03.12)了, 时间有点紧, 老板合作的公司那边又同时需要干活, 真是心力交瘁, 反正只能先把锅甩给师弟们了嘿嘿(逃~)

由于GitHub的私有仓库的开发者最多只能邀请三个, 而GitLab貌似没这限制, 因此此次便先转战GitLab试水, 操作起来不大熟悉, push代码的时候出了点问题……

参考材料
1. 【狀況題】怎麼有時候推不上去…
2. Gitlab强制推送提示”You are not allowed to force push code to a protected branch on this project.”

主要碰到两个问题.

1.

hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

这段讯息远端代码的版本要高于本地版本, 所以Git阻止你进行更新. 解決方法算是有两招.

第一招:先拉再推
因为本地的代码是比較旧的, 所以应该先拉一份远端的代码回來更新, 然后再推一次, 即

git pull --rebase

这里加了–rebase参数是表示代码pull下來之后请使用Rebase方式进行合并, 当然想用一般的合并方式也沒问题. 合并时如果沒发生冲突, 接著应该就可以顺利往上push了.

第二招:无视规则, 总之就是听我的!(大雾~霸道总裁范~)
加上了–force或是-f参数, 它就會强迫硬推上去, 把之前的內容覆盖掉, 即

git push -f

2. Gitlab强制推送提示”You are not allowed to force push code to a protected branch on this project.”

GitLab有受保护的分支一说, 即被保护的分支不允许进行强制推送, 解除掉受保护的分支即可, 然后再次进行强制推送命令即可.

git push -u origin master -f

推完之后再重新保护即可.

其中-u参数的作用是: 如果当前分支与多个主机存在追踪关系, 则可以使用-u选项指定一个默认主机, 这样后面就可以不加任何参数使用git push.

PS: 关于GitLab分支保护的设置可以参考gitLab 分支受保护设置.

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注