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

26 天前
 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'

2306 次点击
所在节点    git
27 条回复
Eddard
26 天前
You are not allowed to force push code to a protected branch on this project.
belin520
26 天前
You are not allowed to force push code to a protected branch on this project.
iOCZS
26 天前
发起合并请求呗
thinkershare
26 天前
gitlab 默认会对主分支执行保护,禁止强制推送到主分支,你可以选择关闭这个选项或者不要这么做,只在自己的私有的分支上 rebase ,不要在主分支上执行 rebase,就正常的做 merge 好了。
NessajCN
26 天前
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
26 天前
正常情况下,就不应该 push -f origin main

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

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

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

特殊需求的话,临时开放下权限
zhtyytg
26 天前
@lichao rebase 之后一般会 force push ,如果 rebase 到 force push 提交完成之间有人 push ,会挤掉然后丢失
alanhe421
26 天前
应该

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

这种问题就是 git flow 姿势不对
furlxy
26 天前
已经说的很清楚了:
You are not allowed to force push code to a protected branch on this project.

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

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

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

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

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

© 2021 V2EX