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

关于 Git rebase 的疑问

  •  
  •   JasonLaw · 2021-10-17 12:10:47 +08:00 · 2828 次点击
    这是一个创建于 893 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我需要修改之前一个 commit 的 message,然后我找到了Changing git commit message after push (given that no one pulled from remote) - Stack Overflow一个回答,我不太理解回答中的以下这段话,请问是什么意思呢?

    Note that if you edit your rebase 'plan' yet it doesn't begin the process of letting you rename the files, run:

    git rebase --continue

    11 条回复    2021-10-18 17:21:41 +08:00
    shiji
        1
    shiji  
       2021-10-17 12:30:11 +08:00 via iPhone   ❤️ 1
    可以看看这个帮助
    https://docs.github.com/cn/get-started/using-git/using-git-rebase-on-the-command-line

    上一步指出要对这几个 commit 做什么( reword ) 因为是交互模式,continue 之后就会根据上一步你的指示开始互动。
    AmoreLee
        2
    AmoreLee  
       2021-10-17 12:30:29 +08:00   ❤️ 1
    操作一下你就明白了。

    执行 git rebase -i HEAD~4

    会出现一个页面,让你标记每个 commit 的顺序,是否需要修改信息等。也就是所谓的 ‘plan’,告诉 git 你想对这些提交干啥。

    如果你设置好你的 ‘plan’, 但是 rebase 没开始,就执行 git rebase --continue 告诉 git 开始 rebase 就好了。


    不过需要注意的是,一旦 rebase 就会改动你的提交记录,如果之前已经提交到远程,rebase 后再想 push 是会报错的。除非你强制推送,但如果别人已经拉取过你的远程仓库,那就会造成很多麻烦。如果只是本地仓库就无所谓了。
    dangyuluo
        3
    dangyuluo  
       2021-10-17 12:39:33 +08:00
    A<-B<-C<-D<-E<-F<-HEAD,你现在在 HEAD 上,发现 C 里面有个错误。你需要
    ```
    git rebase -i C~1 # or git rebase -iB
    ```
    然后修改 rebase 命令,把 C 那行的第一个动词改成 e,然后保存。这时候你就到了 C 提交之后的那一刻,修改好之后(我一般`git commit --amend`),再`git rebase --continue`,之后的 DEF 等 commit 就会自动回来,有冲突的话会停在冲突的地方。

    rebase 还支持其他命令,自己玩玩就行
    skiy
        4
    skiy  
       2021-10-17 14:19:15 +08:00
    @dangyuluo 请问一下,rebase 后,当前项目分支的 commit 就会有冲突了,不允许提交。这个要如何解决呢?
    otakustay
        5
    otakustay  
       2021-10-17 14:36:57 +08:00
    @skiy 无法解决,修改历史必然冲突,如果 push -f 就要通知所有人做好更新
    kaneg
        6
    kaneg  
       2021-10-17 14:42:52 +08:00 via iPhone
    如果已经 push 了,正规途径已经无法更改。除非你获取篡改历史的权限:force push 。
    skiy
        7
    skiy  
       2021-10-17 14:52:56 +08:00
    @otakustay
    @kaneg

    好的,感谢答复。
    CEBBCAT
        8
    CEBBCAT  
       2021-10-18 03:29:40 +08:00 via Android
    @skiy 、再跟远端 rebase 一下就好了,比如 git pull --rebase
    dangyuluo
        9
    dangyuluo  
       2021-10-18 04:20:30 +08:00   ❤️ 1
    @skiy 跟同事打一架,历史是胜利者书写的
    flniu
        10
    flniu  
       2021-10-18 13:37:18 +08:00
    通常情况下 git push -f 是很少很少很少用到的。。
    push 之前随便整,push 之后就不要再想着修改 message 、合并 commit 之类的事情了。否则通知所有人手工修复本地 repo 、手工修复 CI,太麻烦了。
    可工作的软件比 git history 更重要吧。
    corningsun
        11
    corningsun  
       2021-10-18 17:21:41 +08:00
    单人提交的分支可以做,如果是多人合作就建议别改了,会被人骂的。

    使用 fork 客户端更直接

    Interactive Rebase
    Edit, reorder and squash your commits using visual interactive rebase.

    https://git-fork.com/

    https://git-fork.com/images/interactiveRebase.jpg
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   981 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:26 · PVG 04:26 · LAX 13:26 · JFK 16:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.