如何更改一系列提交的作者?
当前回答
这不是对你问题的回答,而是一个脚本,你可以用它来避免将来发生这种情况。它利用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 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#(循环将继续更新上次提交)
我想对@Rognon的回答进行修改。如果所选答案或其他答案不适用于您(在我的特定问题中就是这样),则此答案只是另一种选择:
目标:您将在所有历史记录中用正确的作者修复一个或多个作者,并且您将获得无重复的干净历史记录。此方法的工作原理是将“master”分支替换为“clean”分支(它不使用merge/rebase)
注意:任何使用“主”存储库的人都可能需要在推送之前再次签出(执行这些步骤后),因为合并可能会失败。
我们将使用一个名为“clean”的新分支来执行操作(假设您想修复“master”):
git checkout-b clean
(确保您处于“干净”分支:git分支)
修改以下脚本(替换电子邮件地址和名称)。请注意,此脚本需要两个错误的电子邮件/作者(例如),因此如果您只需要修复一个作者,则可以删除条件的第二部分或将其保持原样(因为它将被忽略,因为它不匹配)。
执行脚本。
#/bin/bash
git filter-branch --force --commit-filter '
if [ "$GIT_COMMITTER_EMAIL" = "wrong1@example.com" -o "$GIT_COMMITTER_EMAIL" = "wrong2@example.com" ];
then
export GIT_COMMITTER_NAME="John Doe";
export GIT_AUTHOR_NAME="John Doe";
export GIT_COMMITTER_EMAIL="correct@example.com";
export GIT_AUTHOR_EMAIL="correct@example.com";
fi;
git commit-tree "$@"
' --tag-name-filter cat -- --all
它必须报告:Ref'refs/heads/clean'被重写。如果报告“未更改”,则脚本中输入的电子邮件可能是错误的。
使用:git log确认历史已更正
如果您正在使用github/gitlab(建议=安全):
在remote中创建“clean”分支:
git push—设置上游原点清洁
将“clean”分支设置为默认分支删除“master”(在执行此操作之前,确保一切都符合预期)。基于“clean”分支创建一个新分支“master”。确认一切正常后,您可以立即删除“clean”分支(也可以重命名它)。
如果您没有使用github/gitlab,或者您更喜欢使用命令:
从本地删除主分支:
git分支-d主
重命名分支:
gitbranch-m清理主机
推它(确保你的“主人”没有受到保护)
git push—强制原始主机
对于重置当前用户和当前时间戳的所有提交(包括首次提交):
git rebase --root --exec "git commit --amend --no-edit --date 'now' --reset-author"
对于windows下的用户,也可以使用git火箭过滤器工具。
根据文档:
更改提交作者姓名和电子邮件:
git-rocket-filter --branch TestBranch --commit-filter '
if (commit.AuthorName.Contains("Jim")) {
commit.AuthorName = "Paul";
commit.AuthorEmail = "paul@company.com";
}
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添加?
- 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之间的区别