V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
git
Pro Git
Atlassian Git Tutorial
Pro Git 简体中文翻译
GitX
chenzw2
V2EX  ›  git

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

  •  
  •   chenzw2 · 12 天前 · 2164 次点击

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

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

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

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

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

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

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

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