[github]修改 commit 的用户名和邮箱

如何修改 commit 的用户名和邮箱

How can I change the author name / email of a commit?

1 修改github的配置文件

记住,修改 github 配置只影响从此以后的 commit

1.1 对全局修改

对所有的 github 项目配置

$ git config --global user.name "liucheng"
$ git config --global user.email "[email protected]"

或者直接在全局配置文件中修改

// ~/.gitconfig
[user]
    name = liucheng
    email = [email protected]

1.2 只对当前项目修改

$ git config user.name "liucheng"
$ git config user.email "[email protected]"

1.3 对下一次的 commit 进行修改

$ git commit --author="liucheng "

2 修改以前的 commit

有三种方法可以堆以前的 commit 进行修改,但是要记住,修改都会影响到线上的项目,如果你的项目有多个合作者,而且基于你没有修改过的项目进行开发,可能会产生严重的问题。
在了解风险后,对此类操作需要三思。

2.1 只修改最近一次的 commit

$ git commit --amend --author="liucheng "

加入 --amend 参数

2.2 使用 rebase 的交互模式

$ git rebase -i -p 0ad14fa5

0ad14fa5 是需要开始更改的 commit hash 值

在打开的 git-rebase-todo 文件中,将 pick 都修改成 edit 关键词
:wq 保存退出

rebase 开始进行 commit 依次信息确认

Stopped at 5772b4bf2... Add images to about page
You can amend the commit now, with 
    git commit --amend 
Once you are satisfied with your changes, run 
    git rebase --continue

现在我们只需要在每个信息确认时,对用户名和邮箱进行修改

$ git commit --amend --author="liucheng "
$ git rebase --continue

2.3 使用 git-filter-branch

参考自 官方教程

git clone --bare https://github.com/*user*/*repo*.git
cd *repo*.git
git filter-branch --env-filter '
WRONG_EMAIL="[email protected]"
NEW_NAME="your username"
NEW_EMAIL="[email protected]"

if [ "$GIT_COMMITTER_EMAIL" = "$WRONG_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$NEW_NAME"
    export GIT_COMMITTER_EMAIL="$NEW_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$WRONG_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$NEW_NAME"
    export GIT_AUTHOR_EMAIL="$NEW_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

注意 if 方括号之间的空格

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

cd ..
rm -rf *repo*.git

你可能感兴趣的:([github]修改 commit 的用户名和邮箱)