Git 修改已提交 commit 的信息

背景

由于 Github 和公司 Git 使用賬號(hào)不一樣,偶爾沒注意,提交出錯(cuò)后就需要修改 commit 信息。

修改最后一次提交 commit 的信息

# 修改最后一次提交的 commit 信息
$ git commit --amend --message="modify message by daodaotest" --author="jiangliheng <jiang_liheng@163.com>"

# 僅修改 message 信息
$ git commit --amend --message="modify message by daodaotest"

# 僅修改 author 信息
$ git commit --amend --author="jiangliheng <jiang_liheng@163.com>"

修改歷史 commit 的信息

操作步驟:

  • git rebase -i <commit id> 列出 commit 列表
  • 找到需要修改的 commit 記錄,把 pick 修改為 edite,:wq 保存退出
  • 修改 commit 的具體信息git commit --amend,保存并繼續(xù)下一條git rebase --continue,直到全部完成
  • 中間也可跳過或退出git rebase (--skip | --abort)
# 列出 rebase 的 commit 列表,不包含 <commit id>
$ git rebase -i <commit id>
# 最近 3 條
$ git rebase -i HEAD~3
# 本地倉(cāng)庫(kù)沒 push 到遠(yuǎn)程倉(cāng)庫(kù)的 commit 信息
$ git rebase -i

# vi 下,找到需要修改的 commit 記錄,```pick``` 修改為 ```edit``` 或 ```e```,```:wq``` 保存退出
# 重復(fù)執(zhí)行如下命令直到完成
$ git commit --amend --message="modify message by daodaotest" --author="jiangliheng <jiang_liheng@163.com>"
$ git rebase --continue

# 中間也可跳過或退出 rebase 模式
$ git rebase --skip
$ git rebase --abort

批量修改歷史 commit 信息

創(chuàng)建批量腳本changeCommit.sh

$ cat changeCommit.sh
#!/bin/sh

git filter-branch --env-filter '

# 之前的郵箱
OLD_EMAIL="jiangliheng@126.com"
# 修改后的用戶名
CORRECT_NAME="jiangliheng"
# 修改后的郵箱
CORRECT_EMAIL="jiangliheng@163.com"

if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

執(zhí)行腳本成功后,強(qiáng)制推送到遠(yuǎn)程服務(wù)器:

$ git push --force --tags origin 'refs/heads/*'

微信公眾號(hào):daodaotest

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請(qǐng)通過簡(jiǎn)信或評(píng)論聯(lián)系作者。

友情鏈接更多精彩內(nèi)容