#Git

如何更改已經 push 的 commit message

Date
2025.05.04
Category
軟體開發
Read Time
3 min
Word
1.0k
Last Mod
2025.05.04
文章 2025.05.04 如何更改已經 push 的 commit message

如何更改已經 push 的 commit message

有時候,我們在 git commit 後才發現 commit message 打錯字,或著是你有一個每個都每個 commit 都寫 “fix bug” 的好朋友。因此這篇文章我們要來討論如何修改已經 push 上去的 commit message。

方法一:如果只想改最後一個 commit

  1. git commit --amend 修改
Bash
git commit --amend

這條指令會打開你的編輯器(像是 Vim),讓你重新編輯最後一個 commit 的訊息。改完後儲存並關閉。

  1. 強制推送
Bash
git push --force

或者更安全一點,只強制更新自己那個分支:

Bash
git push --force-with-lease

這樣就可以成功修改最後一個 commit message。

方法二:想改更早以前的 commit

如果想改好幾個 commit 前,或是一次更改多個訊息,可以用 git rebase

  1. 開始互動式 rebase
Bash
git rebase -i HEAD~n

n 換成你想回溯的 commit 數量,例如改前三個 commit,就用 HEAD~3。你也可以複製 commit hash,像這樣:

Bash
git rebase -i abc1234

這樣會從這個 commit 的下一個開始全部顯示。

  1. 編輯 commit 畫面出來後,你會看到類似:
Text
pick abc1234 First commitpick def5678 Second commitpick ghi9012 Third commit

把想改的那一行的 pick 改成 reword(或是縮寫 r),像這樣:

Text
pick abc1234 First commitr def5678 Second commitpick ghi9012 Third commit

然後儲存並關閉。

  1. 修改訊息 Git 會依序個打開你要 reword 的 commit,讓你編輯新的訊息。編輯完儲存離開就會進入下一個。

  2. Rebase 完成後,強制推送

Bash
git push --force

Bash
git push --force-with-lease

這樣就完成了。記得因為我們強制推送了,所以如果有其他人也在這個分支上工作,他們需要重新拉取這個分支,或者用 git pull --rebase 來更新他們的本地版本。

About me

毛哥EM

這裡是毛哥EM,一隻全端開發龍還有英文辯士。
熱愛開源、音樂、設計、獸迷文化,專研人機互動與人工智慧。

本部落格皆屬原創文章,採 CC BY-SA 4.0 授權,
轉載請註明來自毛哥EM資訊密技。
這篇文章對你有幫助嗎? 考慮幫我買瓶 Red Bull?

毛哥EM 角色插圖

Comments

留言區