git 我可以在以前的所有提交中更改我的名字和姓氏吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4493936/
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
Could I change my name and surname in all previous commits?
提问by Joshua
I would like to change my name, surname and email in my all commits, is it possible?
我想在所有提交中更改我的名字、姓氏和电子邮件,这可能吗?
回答by Josh Lee
Use git-filter-branch
.
git filter-branch --commit-filter 'if [ "$GIT_AUTHOR_NAME" = "Josh Lee" ];
then export GIT_AUTHOR_NAME="Hobo Bob"; export [email protected];
fi; git commit-tree "$@"'
This only affects the author, not the committer (which for most commits will be the same as the author). If you want to rewrite those as well, set the GIT_COMMITTER_NAME
and GIT_COMMITTER_EMAIL
variables.
这仅影响作者,而不影响提交者(对于大多数提交而言,提交者与作者相同)。如果您也想重写它们,请设置GIT_COMMITTER_NAME
和GIT_COMMITTER_EMAIL
变量。
The standard warningabout rewriting history applies; only do it to history that has not yet been shared.
适用于重写历史记录的标准警告;只对尚未共享的历史进行处理。
June 2018 Update
2018 年 6 月更新
The manual now includes a solution, using --env-filter
, in its examples: https://git-scm.com/docs/git-filter-branch#_examples:
该手册现在--env-filter
在其示例中包含一个使用 的解决方案:https: //git-scm.com/docs/git-filter-branch#_examples:
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
回答by user11153
To rewrite both author and commiter in all selected commits:
要在所有选定的提交中重写作者和提交者:
git filter-branch --commit-filter \
'if [ "$GIT_AUTHOR_NAME" = "OldAuthor Name" ]; then \
export GIT_AUTHOR_NAME="Author Name";\
export [email protected];\
export GIT_COMMITTER_NAME="Commmiter Name";\
export [email protected];\
fi;\
git commit-tree "$@"'
回答by denis.peplin
If there are no other authors, you can do:
如果没有其他作者,您可以执行以下操作:
git filter-branch --commit-filter 'export GIT_AUTHOR_NAME="authorname"; \
export [email protected]; git commit-tree "$@"'
回答by Zaz
Save the script below as e.g. ~/.bin/git-replace-author
and run it using, e.g:
将下面的脚本另存为 eg~/.bin/git-replace-author
并使用 eg 运行它:
git replace-author "John Ssmith" "John Smith" "[email protected]"
With no arguments, it updates all commits with your name to use your current email address according to Git config.
在没有参数的情况下,它会根据 Git 配置使用您的姓名更新所有提交以使用您当前的电子邮件地址。
DEFAULT_NAME="$(git config user.name)"
DEFAULT_EMAIL="$(git config user.email)"
export OLD_NAME="${1:-$DEFAULT_NAME}"
export NEW_NAME="${2:-$DEFAULT_NAME}"
export NEW_EMAIL="${3:-$DEFAULT_EMAIL}"
echo "Old:" $OLD_NAME "<*>"
echo "New:" "$NEW_NAME <$NEW_EMAIL>"
echo "To undo, use: git reset $(git rev-parse HEAD)"
git filter-branch --env-filter \
'if [ "$GIT_AUTHOR_NAME" = "${OLD_NAME}" ]; then
export GIT_AUTHOR_NAME="${NEW_NAME}"
export GIT_AUTHOR_EMAIL="${NEW_EMAIL}"
export GIT_COMMITTER_NAME="${NEW_NAME}"
export GIT_COMMITTER_EMAIL="${NEW_EMAIL}"
fi'
Raw(to download)
原始(下载)
回答by EnabrenTane
Only if you haven't pushed your commits to the world. Other wise everyone else has your old name in their repo which is unlikely you can change everyone's.
只有当你没有将你的承诺推向世界时。其他明智的其他人在他们的回购中都有你的旧名字,你不太可能改变每个人的名字。
回答by VonC
With Git 2.24 (Q4 2019), git filter-branch
(and BFG) is deprecated.
在 Git 2.24(2019 年第四季度)中,git filter-branch
(和 BFG)已弃用。
The equivalent would be, using newren/git-filter-repo
, and its example section:
等效的将是, using newren/git-filter-repo
,及其示例部分:
cd repo
git filter-repo --mailmap my-mailmap
with my-mailmap
:
与my-mailmap
:
Correct Name <[email protected]> <[email protected]>
That would replace the author name and email of any commit done by anyone with <[email protected]>
这将替换任何人所做的任何提交的作者姓名和电子邮件 <[email protected]>
See git shortlog
mapping author sectionfor the exact syntax of
有关确切的语法,请参阅git shortlog
映射作者部分