推荐的理论视频:我觉得讲的特别好,基本上讲懂了 git mergegit rebase 的区别

[推荐的实践视频](【git常用操作–git rebase和git merge】https://www.bilibili.com/video/BV1Ur4y1q7xB?vd_source=14bd7726c6d4866de1b04d07b8d59f38):如果看了理论视频有点抽象,那就实践一下就可以了

相关博客

git merge#

特点: 永远向前

这东西很简单,也很暴力

就是将一个分支合并到另一个分支时,然后直接处理,形成新一个 commit ,然后推送

git rebase#

特点: 版本倒退

这东西有点复杂: 比如现在有一个 main分支 和 ecn分支(ecn分支是从main抽出来的)

  1. 共同的起点

    ecn 什么时候 从 main分支中抽离出来,这个起点就是这个

    因此如果 ecn 想要修改信息就只能修改这一段信息

  2. commit 的处理

    commit中有几个东西

    • commit 的推送信息
    • commit 的数量
    • commit 每一个的内容
    • 应该还有其他的,想不出来…

然后 git rebase 就是让用户可以从头版本出发整理修改自己的那些历史版本

如何修改#

  • 第一
1
git log --all

这个命令可以查看你到底有几个commit方便找到自己要修改的多少版本

  • 第二 — 交互式变基

    这个说白了就是修改 commit

1
git rebase -i HEAD~N

N表示你要回头看多少个版本

  • 第三 — 修改操作

    接下来你就会进入一个 编辑界面

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    pick afc06ff chore: compose
    pick 8ad4dfd Delete .gitmodules
    pick af5bfee feat: mod init&update doc
    pick 9373eb5 fix: update secret
    pick c566a67 feat: reg example
    pick 88bddf1 chore: workflow (#1)
    pick bbca6d9 feat: team&membership
    pick 97af1ad fix: config of Postgres
    pick 89422ed feat: teamCreate && teamUpdates
    pick 4468ec5 feat: groupCreate
    pick 926cbf5 feat: groupInfoGet
    pick e9961e4 fix: teamCreate&Update&GetInfo
    pick c0db5be fix: groupCreate
    pick d139d9b fix: GetGroupInfo
    pick f89777b fix: Cleared unnecessary functions in teamDao
    pick 47a47ce chore: dockerfile (#6)

    # Rebase 525456e..47a47ce onto 525456e (16 commands)
    #
    # Commands:
    # p, pick <commit> = use commit
    # r, reword <commit> = use commit, but edit the commit message
    # e, edit <commit> = use commit, but stop for amending
    # s, squash <commit> = use commit, but meld into previous commit
    # f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
    # commit's log message, unless -C is used, in which case
    # keep only this commit's message; -c is same as -C but
    # opens the editor
    # x, exec <command> = run command (the rest of the line) using shell
    # b, break = stop here (continue rebase later with 'git rebase --continue')
    # d, drop <commit> = remove commit
    # l, label <label> = label current HEAD with a name
    # t, reset <label> = reset HEAD to a label
    # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
    # create a merge commit using the original merge commit's
    # message (or the oneline, if no original merge commit was
    # specified); use -c <commit> to reword the commit message
    # u, update-ref <ref> = track a placeholder for the <ref> to be updated
    # to this position in the new commits. The <ref> is
    # updated at the end of the rebase
    #
    # These lines can be re-ordered; they are executed from top to bottom.
    #
    # If you remove a line here THAT COMMIT WILL BE LOST.
    #
    # However, if you remove everything, the rebase will be aborted.
    • pick xxxxx fix: aabcd

      1. pick 这个是命令,每个不同的命令是不同的操作

      2. xxxxx 是哈希值,不用管

      3. fix: aabcd 提交信息

        [!IMPORTANT]

        因此我们如果之前的提交信息有问题,就可以在这里修改:

        我就是因为这样才学了git rebase:因为我把git规范全写成了git fix:

        而真正规范的应该是 feat:

    • Commands: ....

      这个下面的东西都是命令,以及这个命令的解释,具体什么命令干什么自己查gpt吧

    • #

      这个符号 后面的东西都是不需要修改的,需要修改的只有 没有#打头的东西

我们需要做的就是 把自己想要修改的地方 ,pick 改成其他的,例如 r

  • 第四 — 确认自己如何修改

    他会跳出类似的编辑界面:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    # 修改前
    git fix: config of Postgres

    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    #
    # Date: Wed Jul 17 19:35:27 2024 +0800
    #
    # interactive rebase in progress; onto 8ad4dfd
    # Last commands done (6 commands done):
    # pick bbca6d9 feat: team&membership
    # reword b4a45b1 fix: config of Postgres
    # Next commands to do (11 remaining commands):
    # reword 85a3a26 fix: config of Postgres
    # reword 63aa31d feat: teamCreate && teamUpdates
    # You are currently editing a commit while rebasing branch 'dev-ecn' on '8ad4dfd'.
    #
    # Changes to be committed:
    # modified: conf/vars.go
    #
    # # .git/COMMIT_EDITMSG [unix] (23:34 19/07/2024) 1,4 All"/f/InterviewS-RE/.git/COMMIT_EDITMSG" [unix] 19L, 667B

    如果你是想要修改 提交信息,就这样:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    # 修改后
    git fix: 修改后的提交信息

    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    #
    # Date: Wed Jul 17 19:35:27 2024 +0800
    #
    # interactive rebase in progress; onto 8ad4dfd
    # Last commands done (6 commands done):
    # pick bbca6d9 feat: team&membership
    # reword b4a45b1 fix: config of Postgres
    # Next commands to do (11 remaining commands):
    # reword 85a3a26 fix: config of Postgres
    # reword 63aa31d feat: teamCreate && teamUpdates
    # You are currently editing a commit while rebasing branch 'dev-ecn' on '8ad4dfd'.
    #
    # Changes to be committed:
    # modified: conf/vars.go
    #
    # # .git/COMMIT_EDITMSG [unix] (23:34 19/07/2024) 1,4 All"/f/InterviewS-RE/.git/COMMIT_EDITMSG" [unix] 19L, 667B
  • 确认退出

    esc + :wq

    基本上没有只要你标注要修改的版本,都需要你书写一遍如何修改

至此终于修改完毕了

最后 :讲个好玩的事情:
我不小心把小登的仓库pr了,哈哈哈哈,不过他好像版本回退了

如何使用 rebase 删除git history#

需求:从第一张图的效果到第二张图的效果

6377c3adc0faf96f1561fffddc244361

image-20240722001730572

1
git rebase -i HEAD~3    #这里回溯的版本是错误版本的前一版本,相当于为了不让错误发生,先把错误遏制住
1
pick -> edit

ad0db14994e77742dce57283ce237b26

1
git push origin dev-ecn --force-with-lease

完成