我已经在Git中进行了一系列的提交,现在我意识到我忘记正确设置我的用户名和用户电子邮件属性(新机器)。我还没有将这些提交推到我的存储库中,所以在我这样做之前如何纠正这些提交(只有主分支上的3个最新提交)?
我一直在看git reset和git commit -C <id>——reset-author,但我不认为我在正确的轨道上。
我已经在Git中进行了一系列的提交,现在我意识到我忘记正确设置我的用户名和用户电子邮件属性(新机器)。我还没有将这些提交推到我的存储库中,所以在我这样做之前如何纠正这些提交(只有主分支上的3个最新提交)?
我一直在看git reset和git commit -C <id>——reset-author,但我不认为我在正确的轨道上。
当前回答
如果你觉得贬低和修改不安全,你可以这样做。与此同时,你也可以设置全局配置,这可能是你想要做的。
git重置HEAD~(撤销上次提交)
“你的名字”
Git配置——全局用户。电子邮件you@example.com
Git commit -m "message"
其他回答
警告:现在已弃用,支持过滤器-回购。
当你拥有filter-branch的力量时,Rebase/amend似乎效率不高:
git filter-branch --env-filter 'if [ "$GIT_AUTHOR_EMAIL" = "incorrect@email" ]; then
GIT_AUTHOR_EMAIL=correct@email;
GIT_AUTHOR_NAME="Correct Name";
GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL;
GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"; fi' -- --all
(为了清晰起见,这不是必须的)
当你完成时,一定要检查结果,以确保你没有改变任何你不想改变的东西!
我相信你正在寻找的是git rebase -交互式
它允许你重置到一个特定的提交,然后抛出历史更改添加或分组提交
这里有一个解释https://web.archive.org/web/20100213104931/http://blog.madism.org/index.php/2007/09/09/138-git-awsome-ness-git-rebase-interactive
如果你觉得贬低和修改不安全,你可以这样做。与此同时,你也可以设置全局配置,这可能是你想要做的。
git重置HEAD~(撤销上次提交)
“你的名字”
Git配置——全局用户。电子邮件you@example.com
Git commit -m "message"
只在最后一次提交时更改作者:
git commit --amend --author 'Author Name <author.name@mail.com>' --no-edit
假设你只想改变最后N次提交的作者:
git rebase -i HEAD~4 -x "git commit --amend --author 'Author Name <author.name@mail.com>' --no-edit"
笔记
——no-edit标志确保git commit -amend不会要求额外的确认 当你使用git rebase -i时,你可以手动选择要更改作者的提交,
你编辑的文件看起来是这样的:
pick 897fe9e simplify code a little
pick abb60f9 add new feature
exec git commit --amend --author 'Author Name <author.name@mail.com>' --no-edit
pick dc18f70 bugfix
投票最多的答案现在已经过时了。Git在使用Git filter-branch -时显示这个可怕的警告
WARNING: git-filter-branch has a glut of gotchas generating mangled history
rewrites. Hit Ctrl-C before proceeding to abort, then use an
alternative filtering tool such as 'git filter-repo'
(https://github.com/newren/git-filter-repo/) instead.
Filter-repo还不是git的一部分,需要手动安装。
# Requires git v2.22+ and python v3.5+. Check with -
git --version && python3 --version
# Install using pip
pip install git-filter-repo
要只替换以前提交中的电子邮件,运行如下命令-
git filter-repo --email-callback '
return email if email != b"incorrect@email" else b"correct@email"
'
要替换之前提交中的电子邮件和作者名称,运行如下命令-
git filter-repo --commit-callback '
if commit.author_email == b"incorrect@email":
commit.author_email = b"correct@email"
commit.author_name = b"Correct Name"
commit.committer_email = b"correct@email"
commit.committer_name = b"Correct Name"
'
当您在终端中粘贴命令时,请确保有缩进。回调使用python语法,因此缩进很重要。
在文档中阅读更多关于filter-repo回调的信息。