107.10.27 [Git] 修改已經 push 的 commit 訊息

首先把當下的修改做 commit 或 stash
請隨時愛用 git log 和 git status 查看當前狀態
以下為真實例子,要修改的已經 push 而本地端又多兩個 commit

$ git log
commit 452143805e8720f1085a7d14585f07cd6be4cb95 (HEAD -> master)
Author: aben20807 
Date:   Sat Oct 27 19:39:39 2018 +0800

    Implement specialBits()
    
    Because 0xffca3fff has exactly 1 byte 0b00101000
    in the middle, use its complement 0b11010111 to
    shift left 14 bits and complemet it finally.

commit e36bee804ba0bd24a2f4b3658488ca3d5ad11e37
Author: aben20807 
Date:   Sat Oct 27 19:36:43 2018 +0800

    Ignore .swp files

commit 77883e77862f88fc91730ca9a2035fb1d6c5ddf3 (origin/master, origin/HEAD)
Author: aben20807 
Date:   Sat Oct 27 17:32:02 2018 +0800

    Fix shifting warnings from cppcheck
    
    Because cppcheck will complain when shifting signed 32-bit
    value by 31 bits, this commit change these variables that
    needs to shift 31 bits to become unsigned and add the
    integer-suffix 'u' to integer constants.
    For example:
    `x >> 31` becomes `(unsigned) x >> 31`
    `1 >> 31` becomes `1u >> 31`

commit c33896f8fb684227102284a7313228cb876bd716

我要修改 77883e77862f88fc91730ca9a2035fb1d6c5ddf3 的 commit 訊息


則用 rebase -i 前一個的 hash,注意是"前一個"
如下:
$ git rebase -i c33896f8fb684227102284a7313228cb876bd716

此時會出現一個很像 commit 的介面
把要修改的前方的 pick 改成 edit
此圖標題不一樣是因為這是修改後

這時候用 $ git log 就可以發現在要修改的那個 commit 上
使用 $ git commit --amend 進去修改
修改後使用 $ git rebase --continue 就會自動把剛剛後方的東西接上
改完後就會發現在遠端衝突,訊息提示說要 $ git pull,但是不要!
請使用 $ git push -f 強制將本地端的版本 push 上去 (git push -f 有風險請詳閱....)


參考資料:
How do you make changes on a specific commit

沒有留言:

張貼留言

^ Top