我在 Git 中进行了一系列提交,现在我意识到我忘记正确设置我的用户名和用户电子邮件属性(新机器)。我还没有将这些提交推送到我的存储库,那么在我这样做之前如何更正这些提交(只有主分支上的 3 个最新提交)?
我一直在看git reset
and git commit -C <id> --reset-author
,但我认为我没有走在正确的轨道上。
我在 Git 中进行了一系列提交,现在我意识到我忘记正确设置我的用户名和用户电子邮件属性(新机器)。我还没有将这些提交推送到我的存储库,那么在我这样做之前如何更正这些提交(只有主分支上的 3 个最新提交)?
我一直在看git reset
and git commit -C <id> --reset-author
,但我认为我没有走在正确的轨道上。
当您触手可及 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
(为了清楚起见,跨行拆分,但不是必需的)
完成后请务必检查结果,以确保您没有更改任何您无意更改的内容!
与 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"
仅更改最后一次提交的作者:
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
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
git push --force --tags origin 'refs/heads/*'
你就完成了!这里投票最高的答案现在已经过时了。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.
要使用git 建议filter-repo
的命令-
使用 pip 安装命令 -
pip install git-filter-repo
(需要 git v2.22+ 和 python v3.5+。检查git --version && python3 --version
)
修复提交
仅限电子邮件
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 回调的更多信息。
我相信你正在寻找的是git rebase --interactive
它允许您重置到特定的提交,然后抛出历史更改添加或分组提交
如果你正在寻找一个脚本,这个对我来说很方便。
从GitHub下载脚本并将其保存到易于访问的位置。
更改脚本文件的权限以允许其执行:
chmod +x changeauthor.sh
使用不正确的提交历史导航到存储库
cd path/to/repo
运行脚本(带或不带标志)
../path/to/changeauthor.sh --old-email kaka.ruto@example.com \
--new-email ruto.kaka@example.com --new-name "Kaka Ruto" --remote origin
请小心,因为这将重写您当前目录存储库中的所有历史记录!好消息是脚本会为您提供有关您将要执行的操作的警告和信息
在这里阅读更多 https://www.adamdehaven.com/blog/update-commit-history-author-information-for-git-repository/
如果你对贬低和修改感到不安全,你可以这样做。同时,您还将设置您可能打算做的全局配置。
git reset HEAD~
(撤消最后一次提交)
git config --global user.name "Your Name"
git config --global user.email you@example.com
git commit -m "message"