如何更改一系列提交的作者?
当前回答
git rebase -i YOUR_FIRTS_COMMIT_SHA^
while true; do git commit --amend --author="Name Surname <email@example.com>" --no-edit && git rebase --continue; done
在重基完成后按^C#(循环将继续更新上次提交)
其他回答
您可以将其用作别名,以便执行以下操作:
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
希望它有用。
这个答案使用gitfilter分支,文档现在给出了这个警告:gitfilter分支有太多的缺陷,可能会对预期的历史重写造成不明显的影响(而且由于它的性能非常糟糕,因此几乎没有时间调查这些问题)。这些安全和性能问题不能向后兼容地解决,因此不建议使用。请使用其他历史筛选工具,如git filter repo。如果您仍然需要使用git过滤器分支,请仔细阅读安全(和性能)以了解过滤器分支的地雷,然后尽可能小心地避免列出的危险。
更改作者(或提交人)需要重写所有历史。如果您对此感到满意,并认为这是值得的,那么您应该查看gitfilter分支。手册页面包括几个示例,可帮助您入门。还要注意,您可以使用环境变量来更改作者、提交人、日期等的名称——请参阅git手册页面的“环境变量”部分。
具体来说,您可以使用以下命令修复所有分支和标记的所有错误作者名称和电子邮件(来源:GitHub帮助):
#!/bin/sh
git filter-branch --env-filter '
OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
对于使用替代历史过滤工具gitfilter repo,您可以首先安装它并根据gitmailmap的格式构造gitmailmap。
Proper Name <proper@email.xx> Commit Name <commit@email.xx>
然后使用创建的mailmap运行过滤器repo:
git filter-repo --mailmap git-mailmap
以上所有答案都改写了存储库的历史。只要要更改的名称未被多个作者使用,特别是如果存储库已共享且提交已过时,我更愿意使用.mailmap,记录在https://git-scm.com/docs/git-shortlog.它允许将不正确的名称/电子邮件映射到正确的名称,而无需修改回购历史记录。您可以使用以下行:
Proper Name <proper@email.xx> <root@localhost>
GitHub最初有一个很好的解决方案(断开链接),它是以下shell脚本:
#!/bin/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" = "your@email.to.match.example" ]
then
cn="Your New Committer Name"
cm="Your New Committer Email"
fi
if [ "$GIT_AUTHOR_EMAIL" = "your@email.to.match.example" ]
then
an="Your New Author Name"
am="Your New Author Email"
fi
export GIT_AUTHOR_NAME="$an"
export GIT_AUTHOR_EMAIL="$am"
export GIT_COMMITTER_NAME="$cn"
export GIT_COMMITTER_EMAIL="$cm"
'
这不是对你问题的回答,而是一个脚本,你可以用它来避免将来发生这种情况。它利用Git 2.9版以来可用的全局钩子,根据您所在的目录检查您的电子邮件配置:
#!/bin/sh
PWD=`pwd`
if [[ $PWD == *"Ippon"* ]] # 1)
then
EMAIL=$(git config user.email)
if [[ $EMAIL == *"Work"* ]] # 2)
then
echo "";
else
echo "Email not configured to your Work email in the Work directory.";
git config user.email "youremail@youremail.com"
echo "Git email configuration has now been changed to \"$(git config user$
echo "\nPlease run your command again..."
echo ''
exit 1
fi;
elif [[ $PWD == *"Personal"* ]]
then
EMAIL=$(git config user.email)
if [[ $EMAIL == "youremail@youremail.com" ]]
then
echo "";
else
echo "Email is not configured to your personal account in the Personal di$
git config user.email "youremail@youremail.com"
echo "Git email configuration has now been changed to \"$(git config user$
echo "\nPlease run your command again..."
echo ''
exit 1;
fi;
fi;
它检查您当前的工作目录,然后验证您的git是否配置为正确的电子邮件。如果没有,它会自动更改。请在此处查看详细信息。
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的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之间的区别