git rebase 合并多个 commit 后,无法 push 到 origin 上

2024-04-19 11:50:26 +08:00
 chenzw2

git 上有 5 条 commit 记录,我用 git rebase 将 5 条 commit 记录合并成了 1 条,这时使用 git push -f origin main 进行推送却报错:

git push -f origin develop Enumerating objects: 48, done. Counting objects: 100% (48/48), done. Delta compression using up to 12 threads Compressing objects: 100% (19/19), done. Writing objects: 100% (26/26), 4.52 KiB | 385.00 KiB/s, done. Total 26 (delta 12), reused 0 (delta 0), pack-reused 0 remote: GitLab: You are not allowed to force push code to a protected branch on this project. To https://xxx.xxx.xxx.git ! [remote rejected] develop-norm -> develop (pre-receive hook declined) error: failed to push some refs to 'https://xxxx.xxxx.xxxx.git'

3589 次点击
所在节点    git
27 条回复
Eddard
2024-04-19 11:53:00 +08:00
You are not allowed to force push code to a protected branch on this project.
belin520
2024-04-19 11:53:43 +08:00
You are not allowed to force push code to a protected branch on this project.
iOCZS
2024-04-19 12:06:32 +08:00
发起合并请求呗
thinkershare
2024-04-19 12:10:04 +08:00
gitlab 默认会对主分支执行保护,禁止强制推送到主分支,你可以选择关闭这个选项或者不要这么做,只在自己的私有的分支上 rebase ,不要在主分支上执行 rebase,就正常的做 merge 好了。
NessajCN
2024-04-19 12:19:12 +08:00
Git Rebase itself is not seriously dangerous. The real danger cases arise when executing history rewriting interactive rebases and force pushing the results to a remote branch that's shared by other users. This is a pattern that should be avoided as it has the capability to overwrite other remote users' work when they pull.

https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase

rebase 本地用用就好,如果要推到远程仓库会在别人 pull 的时候导致他的自己写的内容丢失
如果这个项目完全只有你自己用,那你删了旧项目新建个新项目更方便
WispZhan
2024-04-19 12:19:36 +08:00
正常情况下,就不应该 push -f origin main

rebase 用自己的的分支 rebase ,然后 提 MR/PR 去 main 或者其他受保护分支

真要 Squash commit 并且像 main 推,那也是在 MR/RP 里做,Gitlab 本来就支持这个操作
chenyu923132714
2024-04-19 12:22:11 +08:00
push -f
siweipancc
2024-04-19 12:58:08 +08:00
@NessajCN 我整天让 githubbot rebase ,罪过罪过
Wien
2024-04-19 13:14:42 +08:00
rebase -i 适合你自己的开发分支整理提交,你自己用随便怎么整。不要在公共分支上用 rebase -i
sagaxu
2024-04-19 13:22:01 +08:00
rebase 到 origin/main 就不会有任何问题,也不需要强推
lichao
2024-04-19 13:25:43 +08:00
公共分支禁止 rebase ,小心被队友暴打
zackzergzeng
2024-04-19 13:59:21 +08:00
你得有权限,并且需要强制 push
ConfusedBiscuit
2024-04-19 14:03:46 +08:00
@lichao +65535 ,我原来就有同事 rebase ,然后差点儿被我们暴打。我就不明白了,为啥都这么喜欢 rebase, rebase, rebase ,难道用 merge 就违法吗
lichao
2024-04-19 14:06:39 +08:00
@ConfusedBiscuit 在自己分支上 rebase 可以使 commit line 干净一些
Leon406
2024-04-19 14:09:11 +08:00
不是管理员不要在公共分支上强推

分支保护就是为了规避这种问题的

特殊需求的话,临时开放下权限
zhtyytg
2024-04-19 14:09:25 +08:00
@lichao rebase 之后一般会 force push ,如果 rebase 到 force push 提交完成之间有人 push ,会挤掉然后丢失
alanhe421
2024-04-19 14:10:07 +08:00
应该

非保护分支(公共分支),rebase with 其它分支的后,提交上去,再 MR 过去。

这种问题就是 git flow 姿势不对
furlxy
2024-04-19 14:14:47 +08:00
已经说的很清楚了:
You are not allowed to force push code to a protected branch on this project.

在自己分支 rebase ,提 merge request 到 master 或者你的 main 分支
daozun
2024-04-19 14:15:41 +08:00
公共分支使用 merge ,在自己分支上使用 rebase 别人的代码
nothingistrue
2024-04-19 14:24:08 +08:00
知道 push -f 命令,却不知道分支可以禁止强制推送,楼主的情况像极了跟着别人的半吊子教程去操作/搞事的人。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1033881

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX