Git 修改已提交的用户名和邮箱

Git 修改已提交的用户名和邮箱

修改上一次提交的邮箱和用户名

git commit --amend --author 'Name'

批量修改多次提交的邮箱和用户名

  1. 新建一个 .sh 脚本在 git 根目录下
  2. .sh脚本内容如下
git filter-branch --env-filter '
an="$GIT_AUTHOR_NAME"
am="$GIT_AUTHOR_EMAIL"
cn="$GIT_COMMITTER_NAME"
cm="$GIT_COMMITTER_EMAIL"
if [ "$GIT_COMMITTER_EMAIL" = "这里写你原来的邮箱" ]
then
	cn="你想替换成的用户名"
	cm="你想替换成的邮箱"
fi
if [ "$GIT_AUTHOR_EMAIL" = "这里写你原来的邮箱" ]
then
	an="你想替换成的用户名"
	am="你想替换成的邮箱"
fi
	export GIT_AUTHOR_NAME="$an"
	export GIT_AUTHOR_EMAIL="$am"
	export GIT_COMMITTER_NAME="$cn"
	export GIT_COMMITTER_EMAIL="$cm"
'
  1. git 根目录下运行即可

修改指定提交

上述脚本中实现了在所有的提交中的修改,如果需要修改指定提交的用户名和邮箱,则需要:
利用指令git rebase -i中的rewordrebase暂停,然后与指令git commit --amend配合,修改用户名和邮箱

Note

The --env-filter option can be used to modify committer and/or author identity. For example, if you found out that your commits have the wrong identity due to a misconfigured user.email, you can make a correction, before publishing the project, like this:

git filter-branch --env-filter '
	if test "$GIT_AUTHOR_EMAIL" = "root@localhost"
	then
		[email protected]
	fi
	if test "$GIT_COMMITTER_EMAIL" = "root@localhost"
	then
		[email protected]
	fi
' -- --all

To restrict rewriting to only part of the history, specify a revision range in addition to the new branch name. The new branch name will point to the top-most revision that a git rev-list of this range will print.

Consider this history:

     D--E--F--G--H
    /     /
A--B-----C

To rewrite only commits D,E,F,G,H, but leave A, B and C alone, use:

git filter-branch ... C..H

To rewrite commits E,F,G,H, use one of these:

git filter-branch ... C..H --not D
git filter-branch ... D..H --not C

Author & Commiter

Author:

git log中的实际编写代码的人,换句话说就是提交log到本地仓库的人。

Commiter:

为将本地仓库push到远端仓库的人

通常情况下 Author 与 Commiter 为同一个人,但是特殊情况下(网络异常…),比如 Author 无法将代码提交到远端仓库,所以请 Commiter 帮忙push到远端仓库

参考

参考了以下链接,在此做了一个更新和总结,感谢各位:
https://hufangyun.com/2018/git-change-user-info/
https://blog.csdn.net/weixin_30729609/article/details/96874653

你可能感兴趣的:(git)