批量修改git commit的作者信息

自己本地環(huán)境全局使用的是公司的郵箱, 另外私人項目使用個人郵箱. 但是自己經(jīng)常忘了設置當前repo的作者, 所以私人項目的代碼提交上去后發(fā)現(xiàn), github并不會認為是我提交的修改. 因此, 想要批量修改commit里面的郵箱.

以下代碼可以修改所有commit的Author郵箱

git filter-branch --env-filter 'export GIT_AUTHOR_EMAIL=new_author_email' --

除了GIT_AUTHOR_EMAIL, 還有GIT_AUTHOR_NAME, GIT_COMMITTER_EMAIL, GIT_COMMITTER_NAME等參數(shù)可以修改.

Committer和Author的區(qū)別?

來自Git文檔

You may be wondering what the difference is between author and committer. The author is the person who originally wrote the patch, whereas the committer is the person who last applied the patch. So, if you send in a patch to a project and one of the core members applies the patch, both of you get credit — you as the author and the core member as the committer.

Author是最初寫這個commit的人, 而committer是提交這個commit的人. 對于私人項目來說, author和committer都是你自己. 但是如果是多人合作項目, 有一個人提交了PR到你的項目中, 最終是你進行的commit, 那么author和committer就會不一樣.

設置本地用戶信息

別忘了把本地的用戶信息改對:

git config user.name "xxx"
git config user.email "xxx@xxx.com"`

想查看是否生效, 可以

git config -l
// OR
git config -e

高端技

自己定義git alias. 方法:

  1. git commit -e, 打開了文件.git/config
  2. 編輯文件, 加入如下這段
[alias]
        # git change-commits GIT_COMMITTER_NAME "old name" "new name"
    change-commits = "!f() { VAR=$1; OLD=$2; NEW=$3; shift 3; git filter-branch --env-filter \"if [[ \\\"$`echo $VAR`\\\" = '$OLD' ]]; then export $VAR='$NEW'; fi\" $@; }; f "

然后保存.

  1. `git change-commits GIT_AUTHOR_EMAIL "old_email" "new_email"

change-commits失敗?

運行git change-commits后報錯:

Cannot create a new backup.
A previous backup already exists in refs/original/
Force overwriting the backup with -f

這是因為在你運行完filter-branch指令后, git幫你保存了舊的代碼庫在.git/refs/original/…, 要么刪了這個文件夾, 要么直接加個-f強制執(zhí)行即可.

參考

  1. Difference between author and committer in Git?
  2. Git, rewrite previous commit usernames and emails
  3. Purging file from Git repo failed, unable to create new backup
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

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