自己本地環(huán)境全局使用的是公司的郵箱, 另外私人項(xiàng)目使用個(gè)人郵箱. 但是自己經(jīng)常忘了設(shè)置當(dāng)前repo的作者, 所以私人項(xiàng)目的代碼提交上去后發(fā)現(xiàn), github并不會認(rèn)為是我提交的修改. 因此, 想要批量修改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是最初寫這個(gè)commit的人, 而committer是提交這個(gè)commit的人. 對于私人項(xiàng)目來說, author和committer都是你自己. 但是如果是多人合作項(xiàng)目, 有一個(gè)人提交了PR到你的項(xiàng)目中, 最終是你進(jìn)行的commit, 那么author和committer就會不一樣.
設(shè)置本地用戶信息
別忘了把本地的用戶信息改對:
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
失敗?
運(yùn)行git change-commits
后報(bào)錯(cuò):
Cannot create a new backup.
A previous backup already exists in refs/original/
Force overwriting the backup with -f
這是因?yàn)樵谀氵\(yùn)行完filter-branch
指令后, git幫你保存了舊的代碼庫在.git/refs/original/…
, 要么刪了這個(gè)文件夾, 要么直接加個(gè)-f
強(qiáng)制執(zhí)行即可.