Git,重写以前的提交用户名和电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2919878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git, rewrite previous commit usernames and emails
提问by JP Silvashy
I've committed a bunch of commits to a project on Github, however I realized I hadn't set up the proper email and committer full name on the computer I'm currently using to make my commits and therefore the users avatar and email address are not there.
我已经向 Github 上的一个项目提交了大量提交,但是我意识到我没有在我目前用来进行提交的计算机上设置正确的电子邮件和提交者全名,因此没有设置用户头像和电子邮件地址不在那里。
How can I rewrite all past commit email and usernames?
如何重写所有过去的提交电子邮件和用户名?
回答by brauliobo
You can add this alias:
您可以添加此别名:
git config 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 "
To change the author name:
要更改作者姓名:
git change-commits GIT_AUTHOR_NAME "old name" "new name"
or the email for only the last 10 commits:
或仅用于最近 10 次提交的电子邮件:
git change-commits GIT_AUTHOR_EMAIL "[email protected]" "[email protected]" HEAD~10..HEAD
Alias:
别名:
change-commits="!f() { VAR=; OLD=; NEW=; shift 3; git filter-branch --env-filter \"if [[ \\"$`echo $VAR`\\" = '$OLD' ]]; then export $VAR='$NEW'; fi\" $@; }; f "
Source: https://github.com/brauliobo/gitconfig/blob/master/configs/.gitconfig
来源:https: //github.com/brauliobo/gitconfig/blob/master/configs/.gitconfig
回答by Olivier Verdier
The solution is already there: Change the author and committer name and e-mail of multiple commits in Git
解决方案已经有了:在 Git 中更改多次提交的作者和提交者姓名和电子邮件
Namely,
即,
git filter-branch -f --env-filter \
"GIT_AUTHOR_NAME='Newname'; GIT_AUTHOR_EMAIL='newemail'; \
GIT_COMMITTER_NAME='committed-name'; GIT_COMMITTER_EMAIL='committed-email';" HEAD
回答by ewall
If you have already pushed some of your commits to the public repository, you do notwant to do this, or it would make an alternate version of the master's history that others may have used. "Don't cross the streams... It would be bad..."
如果你已经迫使一些你提交到公共仓库的,你不希望这样做,否则会令主人的历史的替代版本,其他人可能已经使用。“不要越过溪流……那会很糟糕……”
That said, if it is only the commits you have made to your local repository, then by all means fix this before you push up to the server. You can use the git filter-branch
command with the --commit-filter
option, so it only edits commits which match your incorrect info, like this:
也就是说,如果这只是您对本地存储库所做的提交,那么在您推送到服务器之前一定要解决这个问题。您可以将git filter-branch
命令与--commit-filter
选项一起使用,因此它只会编辑与您的错误信息匹配的提交,如下所示:
git filter-branch --commit-filter '
if [ "$GIT_AUTHOR_EMAIL" = "wrong_email@wrong_host.local" ];
then
GIT_AUTHOR_NAME="Your Name Here (In Lights)";
GIT_AUTHOR_EMAIL="correct_email@correct_host.com";
git commit-tree "$@";
else
git commit-tree "$@";
fi' HEAD
回答by sarusso
After applying Olivier Verdier's answer:
在应用 Olivier Verdier 的回答后:
git filter-branch -f --env-filter \
"GIT_AUTHOR_NAME='Newname'; GIT_AUTHOR_EMAIL='newemail'; \
GIT_COMMITTER_NAME='committed-name'; GIT_COMMITTER_EMAIL='committed-email';" HEAD
...to push the changed history on the original repository use:
...要在原始存储库上推送更改的历史记录,请使用:
git push origin +yourbranch
The above command (note the plus) forces rewriting the history on the original repo as well. Use with caution!
上面的命令(注意加号)也强制重写原始 repo 上的历史记录。谨慎使用!
回答by Jacccck
https://help.github.com/articles/changing-author-info/
https://help.github.com/articles/changed-author-info/
#!/bin/sh
git filter-branch --env-filter '
OLD_EMAIL="[email protected]"
CORRECT_NAME="yourName"
CORRECT_EMAIL="yourEmail"
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
this totally worked for me. After git push, make sure to see update on git's web portal. If the commit was still not linked to my account, shown default thumbnail image next to the commit and it was not reflected on my contributions timeline chart, go to the commit url and append .patch at the end of the url, and verify the name and email are correct.
这完全适合我。在 git push 之后,确保在 git 的门户网站上看到更新。如果提交仍未链接到我的帐户,在提交旁边显示默认缩略图,并且没有反映在我的贡献时间表图表中,请转到提交 url 并在 url 末尾附加 .patch,并验证名称和电子邮件是正确的。
回答by Nick Kuznia
For those that just want the easy copy paste version (aside from updating emails and names):
对于那些只想要简单复制粘贴版本的人(除了更新电子邮件和姓名):
git config 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 "
git change-commits GIT_AUTHOR_NAME "<Old Name>" "<New Name>" -f
git change-commits GIT_AUTHOR_EMAIL <[email protected]> <[email protected]> -f
git change-commits GIT_COMMITTER_NAME "<Old Name>" "<New Name>" -f
git change-commits GIT_COMMITTER_EMAIL <[email protected]> <[email protected]> -f
回答by Pugsley
Considering use of git-filter-branch
is not desired, to do the same thing in git-filter-repo(you may need to install it first with pip install git-filter-repo
):
考虑到使用的git-filter-branch
是没有希望的,做同样的事情在git的过滤器回购(您可能需要与第一次安装它pip install git-filter-repo
):
git-filter-repo --name-callback 'return name.replace(b"OldName", b"NewName")' --email-callback 'return email.replace(b"[email protected]", b"[email protected]")'
If repository is original, w/o remote, you will have to add --force
to force rewrite. (You may want to create backup of your repo before doing this.)
如果存储库是原始的,没有远程,则必须添加--force
以强制重写。(您可能希望在执行此操作之前创建您的存储库的备份。)
If you do not want to preserve refs (they will be displayed in branch history of Git GUI), you will have to add --replace-refs delete-no-add
.
如果您不想保留 refs(它们将显示在 Git GUI 的分支历史记录中),则必须添加--replace-refs delete-no-add
.
For more advanced features, see "Filtering of names & emails".
有关更多高级功能,请参阅“姓名和电子邮件过滤”。
P.S. Stolen and improved from https://stackoverflow.com/a/59591928/714907.