• GIT修改commit信息


    有的时候Git的commit message输入错了,怎么办?怎么回滚?有以下两种场景:

    1. 修改最后一次的log message
    2. 修改历史的log message

    环境准备

    git init
    touch file_b
    ga .
    gcam "commit file_b"
    touch file_c
    ga .
    gcam "commit file_xxxx"
    touch file_d
    ga .
    gcam "commit file_d"

    我使用的是zsh git插件,命令都是简写。

    commit 071673c8bf355162817c9ff1d6232d53e7b2d029
    Author: pengganyu <peng_gy@163.com>
    Date:   Tue Aug 29 09:14:10 2017 +0800
    
        commit file_d
    
    commit b2e67f1f65039aeb18bbf08374444184a827e567
    Author: pengganyu <peng_gy@163.com>
    Date:   Tue Aug 29 09:14:00 2017 +0800
    
        commit file_xxxx
    
    commit 1d9e72fa7ae8287fb6ce5e237dc8be859813348b
    Author: pengganyu <peng_gy@163.com>
    Date:   Tue Aug 29 09:13:45 2017 +0800
    
        commit file_b

    修改上一次log message

    git commit --amend

    git会弹出修改message的信息,直接修改即可

    ## 上一次提交message:commit file_d
    commit file_d_xxxx
    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    #
    # Date:      Tue Aug 29 09:14:10 2017 +0800
    #
    # On branch master
    # Changes to be committed:
    #       new file:   file_d
    #

    修改后 git log信息如下:

    commit 9ab87fac1c9dfd38c0cc7dec838445912b5e45cc
    Author: pengganyu <peng_gy@163.com>
    Date:   Tue Aug 29 09:14:10 2017 +0800
    
        commit file_d_xxx
    
    commit b2e67f1f65039aeb18bbf08374444184a827e567
    Author: pengganyu <peng_gy@163.com>
    Date:   Tue Aug 29 09:14:00 2017 +0800
    
        commit file_xxxx
    
    commit 1d9e72fa7ae8287fb6ce5e237dc8be859813348b
    Author: pengganyu <peng_gy@163.com>
    Date:   Tue Aug 29 09:13:45 2017 +0800
    
        commit file_b

    修改多次历史记录

    git rebase -i commit_id ## 只能修改commit_id 之前的log message
    git rebase -i --root  ## 修改第一次及之前的log message
    git rebase -i HEAD~2   ## 修改倒数第二次及之前的log message
    git rebase -i HEAD~1   ## 修改最后一次提交的log message
    git rebase -i --root
    pick 62252dd commit file_b
    pick b443d79 commit file_xxxx
    pick dcce1df commit file_d_xxx
    
    # Rebase dcce1df onto b88e1cb (3 command(s))
    #
    # Commands:
    # p, pick = use commit
    # r, reword = use commit, but edit the commit message
    # e, edit = use commit, but stop for amending
    # s, squash = use commit, but meld into previous commit
    # f, fixup = like "squash", but discard this commit's log message
    # x, exec = run command (the rest of the line) using shell
    # d, drop = remove commit
    #
    # 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.
    #
    # Note that empty commits are commented out

    将需要修改的某一次提交信息前的pick改为reword,wq保存后即可弹出message的修改界面

    pick 62252dd commit file_b
    reword b443d79 commit file_xxxx
    pick dcce1df commit file_d_xxx

    wq保存后的界面

    commit file_xxxx
    
    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    #
    # Date:      Tue Aug 29 09:14:00 2017 +0800
    #
    # interactive rebase in progress; onto b88e1cb
    # Last commands done (2 commands done):
    #    pick 62252dd commit file_b
    #    reword b443d79 commit file_xxxx
    # Next command to do (1 remaining command):
    #    pick dcce1df commit file_d_xxx
    # You are currently editing a commit while rebasing branch 'master' on 'b88e1cb'.
    #
    # Changes to be committed:
    #       new file:   file_c

    修改message信息如下:

    commit file_xxxx_reword

    保存后的git log 如下

    commit 9942bfeb2cf1415cfb44c83e5ddef13f0351a7b2
    Author: pengganyu <peng_gy@163.com>
    Date:   Tue Aug 29 09:14:10 2017 +0800
    
        commit file_d_xxx
    
    commit b889d15f9d1b8ee8120c9166a7bbb1d8e84715b1
    Author: pengganyu <peng_gy@163.com>
    Date:   Tue Aug 29 09:14:00 2017 +0800
    
        commit file_xxxx_reword
    
    commit eb0c217bfcc941c725df4411ee8d7159937c6b1b
    Author: pengganyu <peng_gy@163.com>
    Date:   Tue Aug 29 09:13:45 2017 +0800
    
        commit file_b

    如果想要修改多个message,将指定的message前的pick修改为reword即可,修改多少个,就会自动弹出多少次的修改界面

    参考资料

    1. Git-Tools-Rewriting-History
  • 相关阅读:
    [原创]平面机器人的避障策略思考
    做个快乐的程序员
    [知识]双音多频(DTMF)信号
    osg 关于LOD
    (3)vtkMapper
    关于坐标系,关于矩阵及线性相关和无关的关系
    osg找不到插件的解决办法
    逆风飞扬,吴仁宏
    整合qt设计师和vs2008出了点问题,记下来
    关于NodeVisitor访问者模式
  • 原文地址:https://www.cnblogs.com/penggy/p/7475808.html
Copyright © 2020-2023  润新知