zoukankan      html  css  js  c++  java
  • git 合并多个commit为1个

    前言

    一般有些bug,只有再生产环境才能复现,所以我们需要多次本地修改,一此次尝试,最终修改好。

    但是之前每次的提交都摆在那儿,很不好看,这几次commit能不能合并成一次呢?

    当时然可以 “合并多个commit为一个完整的commit”

    $ git log
    commit 3da44842224bfefbd6b2ae163b7c6b5c78f7c561 (HEAD -> develop, origin/develop)
    Author: 丁少华 <dingshaohua@aegis-data.cn>
    Date:   Fri Aug 6 15:18:02 2021 +0800
    
        Update README.md4
    commit 47bcad40424414ead71f321edfd97fbe0a6dd05e Author: 丁少华
    <dingshaohua@aegis-data.cn> Date: Fri Aug 6 15:17:54 2021 +0800 Update README.md3
    commit b0622588fab156bf5cfdbdd418de5743035c7a46 Author: 丁少华
    <dingshaohua@aegis-data.cn> Date: Fri Aug 6 15:17:48 2021 +0800 Update README.md2
    commit fe3b82470f3fefa4a6850aa024eac3544dfd4143 Author: 丁少华
    <dingshaohua@aegis-data.cn> Date: Fri Aug 6 15:17:39 2021 +0800 Update README.md1
    commit ae6394121506d27f55f0568809c3c66641012c39 (origin
    /master, master) Author: 丁少华 <dingshaohua@aegis-data.cn> Date: Fri Aug 6 11:44:23 2021 +0800 fix: 修改名字
    commit 15cca3edb6d4bade68207d29ae0c6ed469bea140 Author: 丁少华
    <dingshaohua@aegis-data.cn> Date: Fri Aug 6 11:39:55 2021 +0800 feat: init

    软回退

    软回退,就是将提交信息回退到指定commitid,但是代码会被放在暂存区。如上我们可以这般

    $ git reset --soft ae6394121506d27f55f0568809c3c66641012c39

    $ git log commit ae6394121506d27f55f0568809c3c66641012c39 (HEAD
    -> develop, origin/master, master) Author: 丁少华 <dingshaohua@aegis-data.cn> Date: Fri Aug 6 11:44:23 2021 +0800 fix: 修改名字
    commit 15cca3edb6d4bade68207d29ae0c6ed469bea140 Author: 丁少华
    <dingshaohua@aegis-data.cn> Date: Fri Aug 6 11:39:55 2021 +0800 feat: init

    虽然回退了,但是莫慌,看看你代码,还是最新的,代码并没有回退。代码已经被保存到暂存区了

    然后这个时候再重新commit和强推即可

    git commit -m'feat: 将几次修改readme的提交合并成一个'
    git push -f

    使用变基

    按照题目意思,一共有4次修改某个文件最终成功,那就将这个4次合并

    git rebase -i 命令可以压缩合并多次提交
    格式:git rebase -i [startpoint] [endpoint]
    其中-i的意思是–interactive,即弹出交互式的界面让用户编辑完成合并操作,
    [startpoint] [endpoint]则指定了一个编辑区间,如果不指定[endpoint],则该区间的终点默认是当前分支HEAD所指向的commit

    startpoint不包含自身,所以找到最后第一次修改commitid,再向下找一个
    第一次修改readme文件的下一个commitid是ae6394121506d27f55f0568809c3c66641012c39

    git rebase -i ae6394121506d27f55f0568809c3c66641012c39

    回车,进入下个界面

    pick fe3b824 Update README.md1
    pick b062258 Update README.md2
    pick 47bcad4 Update README.md3
    pick 3da4484 Update README.md4
    
    # Rebase ae63941..3da4484 onto ae63941 (4 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 <commit> = like "squash", but discard this commit's log message
    # 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.
    #
    # 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.

    vim编辑(按下i回车编辑)
    将前3个都改成 squash  意思是 前3次都会被合并到最后一次上

    pick fe3b824 Update README.md1
    squash b062258 Update README.md2
    squash 47bcad4 Update README.md3
    squash 3da4484 Update README.md4

    然后 wq! 保存即可
    接下来就会自动弹出下个界面

    # This is a combination of 4 commits.
    # This is the 1st commit message:
    
    Update README.md1
    # This is the commit message #2:
    
    Update README.md2
    # This is the commit message #3:
    
    Update README.md3
    # This is the commit message #4:
    
    Update README.md4
    
    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    #
    # Date:      Fri Aug 6 15:17:39 2021 +0800
    #
    # interactive rebase in progress; onto ae63941
    # Last commands done (4 commands done):
    #    squash 47bcad4 Update README.md3
    #    squash 3da4484 Update README.md4
    # No commands remaining.
    # You are currently rebasing branch 'develop' on 'ae63941'.
    #
    # Changes to be committed:
    #       modified:   README.md
    #

    设置commit信息,你可以不修改,也可以修改,这里我修改了,如下

    # This is a combination of 4 commits.
    # This is the 1st commit message:
    fix: 使用rebase方式将4次提交合并成1个
    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    #
    # Date:      Fri Aug 6 15:17:39 2021 +0800
    #
    # interactive rebase in progress; onto ae63941
    # Last commands done (4 commands done):
    #    squash 47bcad4 Update README.md3
    #    squash 3da4484 Update README.md4
    # No commands remaining.
    # You are currently rebasing branch 'develop' on 'ae63941'.
    #
    # Changes to be committed:
    #       modified:   README.md

    然后wq!保存

    然后git log查看 是不是合并了已经?

    最后强推 直接提交即可

    git push -f



  • 相关阅读:
    pycharm使用小技巧
    多线程的异常处理
    零星
    python的多线程 ThreadPoolExecutor
    零星
    python的轮询timer 和js的轮询setInterval
    fcitx 输入框纵向
    Gvim配置
    窗口,父窗口parentwindow,所有者窗口ownerwindow
    Request对象 --web浏览器向web服务端的请求
  • 原文地址:https://www.cnblogs.com/dshvv/p/15109013.html
Copyright © 2011-2022 走看看