如何更改一系列提交的作者?
当前回答
您可以将其用作别名,以便执行以下操作:
git change-commits GIT_AUTHOR_NAME "old name" "new name"
或最近10次提交:
git change-commits GIT_AUTHOR_EMAIL "old@email.com" "new@email.com" HEAD~10..HEAD
添加到~/.gitconfig:
[alias]
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 "
资料来源:https://github.com/brauliobo/gitconfig/blob/master/configs/.gitconfig
希望它有用。
其他回答
你的问题很普遍。请参阅“使用Mailmap修复Git中的作者列表”
为了简单起见,我创建了一个脚本来简化这个过程:gitchangemail
将该脚本放到路径上后,可以发出以下命令:
更改当前分支上的作者匹配$git changemail-aold@email.com-n新名称-mnew@email.com更改<branch>和<branch2>上的作者和提交者匹配。将-f传递到筛选器分支以允许重写备份$git changemail-bold@email.com-n新名称-mnew@email.com---f<;分支><;分支2>显示回购中的现有用户$git changemail--显示两者
顺便说一下,在进行更改后,使用:gitbackupclean从过滤器分支中清除备份
如果只有前几次提交的作者不好,您可以使用exec命令和--modify commit在git rebase-i内部执行此操作,如下所示:
git rebase -i HEAD~6 # as required
它为您提供了可编辑的提交列表:
pick abcd Someone else's commit
pick defg my bad commit 1
pick 1234 my bad commit 2
然后添加exec--author=“…”在所有作者不好的行之后:
pick abcd Someone else's commit
pick defg my bad commit 1
exec git commit --amend --author="New Author Name <email@address.example>" -C HEAD
pick 1234 my bad commit 2
exec git commit --amend --author="New Author Name <email@address.example>" -C HEAD
保存并退出编辑器(运行)。
这个解决方案可能比其他解决方案键入的时间更长,但它是高度可控的——我确切地知道它命中了什么提交。
感谢@asmeurer的灵感。
对于所有提交,我的解决方案:
git rebase -i --root -x "git commit --amend --author 'bedorlan <bedorlan@gmail.com>' --no-edit"
正如docgnome所提到的,重写历史是危险的,会破坏其他人的知识库。
但是,如果您真的想这样做,并且您处于bash环境中(在Linux和Windows中没有问题,您可以使用git bash,这是安装git时提供的),请使用gitfilter分支:
git filter-branch --env-filter '
if [ $GIT_AUTHOR_EMAIL = bad@email ];
then GIT_AUTHOR_EMAIL=correct@email;
fi;
export GIT_AUTHOR_EMAIL'
要加快速度,可以指定要重写的修订范围:
git filter-branch --env-filter '
if [ $GIT_AUTHOR_EMAIL = bad@email ];
then GIT_AUTHOR_EMAIL=correct@email;
fi;
export GIT_AUTHOR_EMAIL' HEAD~20..HEAD
git过滤器分支的一个更安全的替代方案是git文档建议的过滤器回购工具。
git filter-repo --commit-callback '
old_email = b"your-old-email@example.com"
correct_name = b"Your Correct Name"
correct_email = b"your-correct-email@example.com"
if commit.committer_email == old_email :
commit.committer_name = correct_name
commit.committer_email = correct_email
if commit.author_email == old_email :
commit.author_name = correct_name
commit.author_email = correct_email
'
上述命令反映了此脚本中使用的逻辑,但使用过滤器repo而不是过滤器分支。
提交后回调选项的代码体基本上是用于处理提交的python代码。您可以在这里用python编写自己的逻辑。请在此处查看有关提交对象及其属性的更多信息。
由于filter repo工具未与git捆绑,您需要单独安装它。
请参阅先决条件和安装指南
如果您有一个python-env>=3.5,可以使用pip来安装它。
pip3 install git-filter-repo
注意:强烈建议在新克隆上尝试使用过滤器回购工具。操作完成后,也会移除遥控器。阅读更多有关为什么删除遥控器的信息。另请阅读INTERNALS部分中此工具的限制。
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别