git
› Pro Git
› Atlassian Git Tutorial
› Pro Git 简体中文翻译
› GitX
chenzw2
V2EX  ›  git

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

  •  
  •   chenzw2 · Apr 19, 2024 · 4758 views
    This topic created in 929 days ago, the information mentioned may be changed or developed.

    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'

    27 replies  •  2024-04-21 09:19:18 +08:00
    Eddard
    Eddard
    Apr 19, 2024
    1
    You are not allowed to force push code to a protected branch on this project.
    icanfork
    icanfork
    Apr 19, 2024
    2
    You are not allowed to force push code to a protected branch on this project.
    iOCZS
    iOCZS
    Apr 19, 2024
    3
    发起合并请求呗
    thinkershare
    thinkershare
    Apr 19, 2024
    4
    gitlab 默认会对主分支执行保护,禁止强制推送到主分支,你可以选择关闭这个选项或者不要这么做,只在自己的私有的分支上 rebase ,不要在主分支上执行 rebase,就正常的做 merge 好了。
    NessajCN
    NessajCN
    Apr 19, 2024
    5
    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
    WispZhan
    Apr 19, 2024❤️1
    6
    正常情况下,就不应该 push -f origin main

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

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

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

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

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

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

    在自己分支 rebase ,提 merge request 到 master 或者你的 main 分支
    daozun
    daozun
    Apr 19, 2024❤️1
    19
    公共分支使用 merge ,在自己分支上使用 rebase 别人的代码
    nothingistrue
    nothingistrue
    Apr 19, 2024
    20
    知道 push -f 命令,却不知道分支可以禁止强制推送,楼主的情况像极了跟着别人的半吊子教程去操作/搞事的人。
    chenzw2
    chenzw2
    OP
    Apr 19, 2024
    21
    可以了,设置中临时允许强制推送,推送成功,感谢各位大佬!
    lovelylain
    lovelylain
    Apr 19, 2024 via Android
    22
    rebase 后要 push -f ,一般用于发布前把 master 分支最新修改合并进开发分支且使开发分支的提交记录清爽,如果开发分支也不允许 push -f 的话,就不能用 reabse
    2123123
    2123123
    Apr 19, 2024
    23
    我认为 rebase -i 跟 rebase 完全不是一回事也没必要混到一起说
    平时使用 pull --rebase 或者用 rebase 正确合并两个分支不会需要用到 force push
    与 merge 不同,rebase 是有顺序的
    ooops
    ooops
    Apr 20, 2024
    24
    即使 --force 也要用 --force-with-lease
    kneo
    kneo
    Apr 20, 2024 via Android
    25
    自己私人的项目,并且完全没有别人 clone 过,可以强制 push ,其它情况不建议。
    buhaoban
    buhaoban
    Apr 20, 2024
    26
    通过 PR 方式来 force push, 这样既可以保护 main/master, 也方便以 PR 为单位管理功能开发。
    guanzhangzhang
    guanzhangzhang
    Apr 21, 2024
    27
    哈哈哈,仔细看看报错,就是不允许强推,在多人合作和项目开发里,release 和 master/main 是有保护的,都是自己分支上 rebase 成一个 commit ,或者 git pull --rebase origin/master 后再 push -f 的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5163 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 94ms · UTC 06:38 · PVG 14:38 · LAX 22:38 · JFK 01:38
    ♥ Do have faith in what you're doing.