背景
由于 Github 和公司 Git 使用賬号不一樣,偶爾沒注意,送出出錯後就需要修改 commit 資訊。
修改最後一次送出 commit 的資訊
# 修改最近送出的 commit 資訊
$ git commit --amend --message="modify message by daodaotest" --author="jiangliheng <[email protected]>"
# 僅修改 message 資訊
$ git commit --amend --message="modify message by daodaotest"
# 僅修改 author 資訊
$ git commit --amend --author="jiangliheng <[email protected]>"
修改曆史送出 commit 的資訊
操作步驟:
- git rebase -i 列出 commit 清單
- 找到需要修改的 commit 記錄,把 pick 修改為 edit 或 e,:wq 儲存退出
-
修改 commit 的具體資訊git commit --amend,儲存并繼續下一條git rebase
–continue,直到全部完成
- 中間也可跳過或退出git rebase (–skip | --abort)
# 列出 rebase 的 commit 清單,不包含 <commit id>
$ git rebase -i <commit id>
# 最近 3 條
$ git rebase -i HEAD~3
# 本地倉庫沒 push 到遠端倉庫的 commit 資訊
$ git rebase -i
# vi 下,找到需要修改的 commit 記錄,```pick```修改為 ```edit```或 ```e```,```:wq```儲存退出
# 重複執行如下指令直到完成
$ git commit --amend --message="modify message by daodaotest" --author="jiangliheng <[email protected]>"
$ git rebase --continue
# 中間也可跳過或退出 rebase 模式
$ git rebase --skip
$ git rebase --abort
批量修改曆史 commit 資訊
建立批量腳本changeCommit.sh:
$ cat changeCommit.sh
#!/bin/sh
git filter-branch --env-filter '
# 之前的郵箱
OLD_EMAIL="[email protected]"
# 修改後的使用者名
CORRECT_NAME="jiangliheng"
# 修改後的郵箱
CORRECT_EMAIL="[email protected]"
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
執行腳本成功後,強制推送到遠端伺服器:
$ git push --force --tags origin 'refs/heads/*'
參考連結 :
https://mp.weixin.qq.com/s/Uu6EnOEnroPswr5csQKrMQ