git rebase 合并多个 commit 后,无法 push 到 origin 上 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
git
Pro Git
Atlassian Git Tutorial
Pro Git 简体中文翻译
GitX
chenzw2
V2EX    git

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

  •  
  •   chenzw2 2024-04-19 11:50:26 +08:00 3680 次点击
    这是一个创建于 547 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

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

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

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

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

    这种问题就是 git flow 姿势不对
    furlxy
        18
    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
        19
    daozun  
       2024-04-19 14:15:41 +08:00   1
    公共分支使用 merge ,在自己分支上使用 rebase 别人的代码
    nothingistrue
        20
    nothingistrue  
       2024-04-19 14:24:08 +08:00
    知道 push -f 命令,却不知道分支可以禁止强制推送,楼主的情况像极了跟着别人的半吊子教程去操作/搞事的人。
    chenzw2
        21
    chenzw2  
    OP
       2024-04-19 14:25:59 +08:00
    可以了,设置中临时允许强制推送,推送成功,感谢各位大佬!
    lovelylain
        22
    lovelylain  
       2024-04-19 14:46:11 +08:00 via Android
    rebase 后要 push -f ,一般用于发布前把 master 分支最新修改合并进开发分支且使开发分支的提交记录清爽,如果开发分支也不允许 push -f 的话,就不能用 reabse
    2123123
        23
    2123123  
       2024-04-19 16:59:43 +08:00
    我认为 rebase -i 跟 rebase 完全不是一回事也没必要混到一起说
    平时使用 pull --rebase 或者用 rebase 正确合并两个分支不会需要用到 force push
    与 merge 不同,rebase 是有顺序的
    ooops
        24
    ooops  
       2024-04-20 01:18:55 +08:00
    即使 --force 也要用 --force-with-lease
    kneo
        25
    kneo  
       2024-04-20 10:43:50 +08:00 via Android
    自己私人的项目,并且完全没有别人 clone 过,可以强制 push ,其它情况不建议。
    buhaoban
        26
    buhaoban  
       2024-04-20 16:04:33 +08:00
    通过 PR 方式来 force push, 这样既可以保护 main/master, 也方便以 PR 为单位管理功能开发。
    guanzhangzhang
        27
    guanzhangzhang  
       2024-04-21 09:19:18 +08:00
    哈哈哈,仔细看看报错,就是不允许强推,在多人合作和项目开发里,release 和 master/main 是有保护的,都是自己分支上 rebase 成一个 commit ,或者 git pull --rebase origin/master 后再 push -f 的
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2490 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 11:10 PVG 19:10 LAX 04:10 JFK 07:10
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86