我已经在Git中进行了一系列的提交,现在我意识到我忘记正确设置我的用户名和用户电子邮件属性(新机器)。我还没有将这些提交推到我的存储库中,所以在我这样做之前如何纠正这些提交(只有主分支上的3个最新提交)?
我一直在看git reset和git commit -C <id>——reset-author,但我不认为我在正确的轨道上。
我已经在Git中进行了一系列的提交,现在我意识到我忘记正确设置我的用户名和用户电子邮件属性(新机器)。我还没有将这些提交推到我的存储库中,所以在我这样做之前如何纠正这些提交(只有主分支上的3个最新提交)?
我一直在看git reset和git commit -C <id>——reset-author,但我不认为我在正确的轨道上。
当前回答
只在最后一次提交时更改作者:
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 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-filter-repo。
然而,要简单地更改作者名称和/或电子邮件,可以使用——mailmap或——use-mailmap来代替回调。
您需要根据格式创建一个mailmap文件(参见git-filter-repo/docs或git-scm.com/docs)
然后简单地运行
git filter-repo --mailmap .mailmap
投票最多的答案现在已经过时了。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回调的信息。
当与exec结合使用时,交互式rebase方法非常好。您可以针对rebase中的特定提交或所有提交运行任何shell命令。
首先设置git作者设置
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
然后为给定SHA之后的所有提交重置作者
git rebase -i YOUR_SHA -x "git commit --amend --reset-author -CHEAD"
这将弹出编辑器确认更改。您在这里需要做的就是保存并退出,它将通过每次提交并运行-x标志中指定的命令。
根据下面@Dave的评论,你也可以在保持原始时间戳的同时更改作者:
git rebase -i YOUR_SHA -x "git commit --amend --author 'New Name <new_address@example.com>' -CHEAD"
这个方法被GitHub记录下来正是为了这个目的(尽管GitHub已经删除了它)。步骤如下:
打开终端,并使您的回购的一个裸克隆
git clone --bare https://github.com/user/repo.git
cd repo
编辑以下脚本(替换OLD_EMAIL、CORRECT_EMAIL和CORRECT_NAME)
#!/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
复制/粘贴脚本到您的终端,并按enter键运行它。 用git Push——force——tags origin 'refs/heads/*'来推动你的更改,你就完成了!