自己本地環(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ū)別?
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. 方法:
-
git commit -e, 打開了文件.git/config - 編輯文件, 加入如下這段
[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 "
然后保存.
- `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í)行即可.