如何更改一系列提交的作者?
注意:此答案会更改 SHA1,因此在已推送的分支上使用它时要小心。如果您只想修正姓名的拼写或更新旧电子邮件,Git 允许您执行此操作,而无需使用 .mailmap
重写历史记录。请参阅my other answer。
使用变基
首先,如果您还没有这样做,您可能希望在 git-config 中修复您的名字:
git config --global user.name "New Author Name"
git config --global user.email "<email@address.example>"
这是可选的,但它也将确保重置提交者名称,假设这是您需要的。
要使用变基重写一系列提交的元数据,请执行
git rebase -r <some commit before all of your bad commits> \
--exec 'git commit --amend --no-edit --reset-author'
--exec
将在每次提交被重写后运行 git commit
步骤(就像您重复运行 git commit && git rebase --continue
一样)。
如果您还想更改您的第一个提交(也称为“根”提交),则必须将 --root
添加到 rebase 调用。
这会将提交者和作者都更改为您的 user.name
/user.email
配置。如果您不想更改该配置,可以使用 --author "New Author Name <email@address.example>"
而不是 --reset-author
。请注意,这样做不会更新提交者——只是作者。
单次提交
如果您只想更改最近的提交,则不需要 rebase。只需修改提交:
git commit --amend --no-edit --reset-author
对于较旧的 Git 客户端(2020 年 7 月之前)
-r,--rebase-merges
可能不适合您。作为替代,您可以使用 -p
。请注意,-p
存在严重问题,现已弃用。
这个答案使用了 git-filter-branch,文档现在给出了这个警告: git filter-branch 有很多陷阱,可以产生不明显的预期历史重写的错误(并且可以让你几乎没有时间去调查这样问题,因为它的性能如此糟糕)。这些安全和性能问题不能向后兼容修复,因此不建议使用它。请使用替代的历史过滤工具,例如 git filter-repo。如果您仍然需要使用 git filter-branch,请仔细阅读安全(和性能)以了解 filter-branch 的地雷,然后尽可能合理地避免其中列出的许多危害。
更改作者(或提交者)将需要重写所有历史记录。如果您对此感到满意并认为值得,那么您应该查看 git filter-branch。手册页包含几个示例以帮助您入门。另请注意,您可以使用环境变量来更改作者、提交者、日期等的名称——请参阅 git manual page 的“环境变量”部分。
具体来说,您可以使用以下命令修复所有分支和标签的所有错误作者姓名和电子邮件(来源:GitHub help):
#!/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 filter-repo,您可以先安装它并按照gitmailmap的格式构造一个git-mailmap
。
Proper Name <proper@email.xx> Commit Name <commit@email.xx>
然后使用创建的邮件映射运行 filter-repo:
git filter-repo --mailmap git-mailmap
git push --force --tags origin HEAD:master
解决
git update-ref -d refs/original/refs/heads/master
将其删除,然后检查 .git/refs/original
文件夹结构是否为空,然后使用 rm -rf .git/refs/original
将其删除。最后,您可以通过以下方式验证新的重写日志:git log --pretty=format:"[%h] %cd - Committer: %cn (%ce), Author: %an (%ae)"
!还有一件事:.git/logs
有一些日志文件仍然使用您的旧名称!
一个班轮,但如果您有一个多用户存储库,请小心 - 这会将所有提交更改为具有相同的(新)作者和提交者。
git filter-branch -f --env-filter "GIT_AUTHOR_NAME='Newname'; GIT_AUTHOR_EMAIL='new@email'; GIT_COMMITTER_NAME='Newname'; GIT_COMMITTER_EMAIL='new@email';" HEAD
在字符串中使用换行符(这在 bash 中是可能的):
git filter-branch -f --env-filter "
GIT_AUTHOR_NAME='Newname'
GIT_AUTHOR_EMAIL='new@email'
GIT_COMMITTER_NAME='Newname'
GIT_COMMITTER_EMAIL='new@email'
" HEAD
HEAD
,为什么它会重写所有提交?
git push --force --tags origin 'refs/heads/*'
$git push --force --tags origin 'refs/heads/master'
$git push --force --tags origin 'refs/heads/main'
你也可以这样做:
git filter-branch --commit-filter '
if [ "$GIT_COMMITTER_NAME" = "<Old Name>" ];
then
GIT_COMMITTER_NAME="<New Name>";
GIT_AUTHOR_NAME="<New Name>";
GIT_COMMITTER_EMAIL="<New Email>";
GIT_AUTHOR_EMAIL="<New Email>";
git commit-tree "$@";
else
git commit-tree "$@";
fi' HEAD
请注意,如果您在 Windows 命令提示符下使用此命令,则需要使用 "
而不是 '
:
git filter-branch --commit-filter "
if [ "$GIT_COMMITTER_NAME" = "<Old Name>" ];
then
GIT_COMMITTER_NAME="<New Name>";
GIT_AUTHOR_NAME="<New Name>";
GIT_COMMITTER_EMAIL="<New Email>";
GIT_AUTHOR_EMAIL="<New Email>";
git commit-tree "$@";
else
git commit-tree "$@";
fi" HEAD
"A previous backup already exists in refs/original/ Force overwriting the backup with -f"
抱歉,但是当您执行此脚本两次时,-f
-flag 将在哪里。实际上,这是在布赖恩的回答中,对于过滤器分支之后的干扰是解决方案感到抱歉。
当您没有初始化 $HOME/.gitconfig
时会发生这种情况。您可以将其修复为:
git config --global user.name "you name"
git config --global user.email you@domain.example
git commit --amend --reset-author
使用 Git 版本 1.7.5.4 进行测试。
请注意,这仅修复了最后一次提交。
--local
也可以
git config
事后设置正确的作者,git commit --amend --reset-author --no-edit
命令特别有用。当我不得不更新我的电子邮件时,刚刚节省了我的 a$$。
如果只有前几个提交的作者不好,您可以使用 exec
命令和 --amend
提交在 git rebase -i
内执行所有操作,如下所示:
git rebase -i HEAD~6 # as required
它为您提供了可编辑的提交列表:
pick abcd Someone else's commit
pick defg my bad commit 1
pick 1234 my bad commit 2
然后在所有作者不好的行之后添加 exec ... --author="..."
行:
pick abcd Someone else's commit
pick defg my bad commit 1
exec git commit --amend --author="New Author Name <email@address.example>" -C HEAD
pick 1234 my bad commit 2
exec git commit --amend --author="New Author Name <email@address.example>" -C HEAD
保存并退出编辑器(运行)。
这个解决方案的打字时间可能比其他解决方案长,但它是高度可控的——我确切地知道它命中了什么提交。
感谢@asmeurer 的启发。
exec git commit --amend --reset-author -C HEAD
吗?
git rebase -i HEAD^^^^^^
你也可以写 git rebase -i HEAD~6
fatal: Needed a single revision
,请尝试 git rebase -i --root
对于单个提交:
git commit --amend --author="Author Name <email@address.example>"
(摘自 asmeurer 的回答)
git help commit
,git commit --amend
在“当前分支的尖端”(即 HEAD)更改提交。这通常是最近的提交,但您可以通过第一个使用 git checkout <branch-name>
或 git checkout <commit-SHA>
提交的 checking out 使其成为您想要的任何提交。
author
而不是 committer
GitHub 最初有一个很好的解决方案 (broken link),它是以下 shell 脚本:
#!/bin/sh
git filter-branch --env-filter '
an="$GIT_AUTHOR_NAME"
am="$GIT_AUTHOR_EMAIL"
cn="$GIT_COMMITTER_NAME"
cm="$GIT_COMMITTER_EMAIL"
if [ "$GIT_COMMITTER_EMAIL" = "your@email.to.match.example" ]
then
cn="Your New Committer Name"
cm="Your New Committer Email"
fi
if [ "$GIT_AUTHOR_EMAIL" = "your@email.to.match.example" ]
then
an="Your New Author Name"
am="Your New Author Email"
fi
export GIT_AUTHOR_NAME="$an"
export GIT_AUTHOR_EMAIL="$am"
export GIT_COMMITTER_NAME="$cn"
export GIT_COMMITTER_EMAIL="$cm"
'
git reset --hard HEAD^
几次才能将它们升级到早期版本,git pull
-ed 修改后的版本,这里我没有任何包含 unknown <stupid-windows-user@.StupidWindowsDomain.local>
的行(喜欢 git 的默认设置) .
git push -f
。此外,在此之后必须重新克隆本地存储库。
用于更改最后 N 次提交的作者的单个命令:
git rebase -i HEAD~N -x "git commit --amend --author 'Author Name <author.name@mail.example>' --no-edit"
笔记
将 HEAD~N 替换为参考,直到您要重写提交。这可以是哈希、HEAD~4、分支名称、...
--no-edit 标志确保 git commit --amend 不要求额外确认
当您使用 git rebase -i 时,您可以手动选择更改作者的提交,
您编辑的文件将如下所示:
pick 897fe9e simplify code a little
exec git commit --amend --author 'Author Name <author.name@mail.example>' --no-edit
pick abb60f9 add new feature
exec git commit --amend --author 'Author Name <author.name@mail.example>' --no-edit
pick dc18f70 bugfix
exec git commit --amend --author 'Author Name <author.name@mail.example>' --no-edit
然后,您仍然可以修改一些行以查看要更改作者的位置。这为您在自动化和控制之间提供了一个很好的中间地带:您会看到将要运行的步骤,一旦您保存,所有内容都会立即应用。
请注意,如果您已经使用 git config user.name <your_name>
和 git config user.email <your_email>
修复了作者信息,您还可以使用此命令:
git rebase -i HEAD~N -x "git commit --amend --reset-author --no-edit"
--root
而不是 HEAD~N
来编辑整个历史记录(包括初始提交),并使用 --reset-author
而不是 --author ...
来获取当前提交者
HEAD~4
替换为引用,直到您要重写提交的位置...我会尝试在我的回答中使这一点更清楚。正如我之前提到的:当心合并提交,你会遇到复杂的东西
正如 docgnome 所提到的,重写历史是危险的,并且会破坏其他人的存储库。
但是如果你真的想这样做并且你在一个 bash 环境中(在 Linux 上没问题,在 Windows 上,你可以使用 git bash,它是 git 安装提供的),使用 git filter-branch:
git filter-branch --env-filter '
if [ $GIT_AUTHOR_EMAIL = bad@email ];
then GIT_AUTHOR_EMAIL=correct@email;
fi;
export GIT_AUTHOR_EMAIL'
为了加快速度,您可以指定要重写的修订范围:
git filter-branch --env-filter '
if [ $GIT_AUTHOR_EMAIL = bad@email ];
then GIT_AUTHOR_EMAIL=correct@email;
fi;
export GIT_AUTHOR_EMAIL' HEAD~20..HEAD
--tag-name-filter cat
是“让它工作”选项。
--tag-name-filter cat
。这确实应该是默认行为。
您可以将其用作别名,以便执行以下操作:
git change-commits GIT_AUTHOR_NAME "old name" "new name"
或最后 10 次提交:
git change-commits GIT_AUTHOR_EMAIL "old@email.com" "new@email.com" HEAD~10..HEAD
添加到 ~/.gitconfig:
[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 "
来源:https://github.com/brauliobo/gitconfig/blob/master/configs/.gitconfig
希望它是有用的。
[[ ]]
替换为 sh 兼容的测试 [ ]
(单括号)。除此之外它工作得很好,谢谢!
当从另一个作者那里接管一个未合并的提交时,有一种简单的方法来处理这个问题。
git commit --amend --reset-author
--no-edit
以使这更容易,因为通常大多数人只想更新电子邮件地址而不是提交消息
我应该指出,如果唯一的问题是作者/电子邮件与您平时不同,这不是问题。正确的解决方法是在目录的底部创建一个名为 .mailmap
的文件,其中包含如下行
Name you want <email you want> Name you don't want <email you don't want>
从那时起,像 git shortlog
这样的命令将认为这两个名称相同(除非您明确告诉他们不要这样做)。有关详细信息,请参阅 https://schacon.github.io/git/git-shortlog.html。
这具有此处所有其他解决方案的优势,因为您不必重写历史记录,如果您有上游可能会导致问题,并且始终是意外丢失数据的好方法。
当然,如果您以自己的身份提交了某些内容,并且确实应该是其他人,并且此时您不介意重写历史记录,那么更改提交作者可能是出于归因目的的好主意(在这种情况下,我将您引导至我的其他答案here)。
这是@Brian 版本的更详细的版本:
要更改作者和提交者,您可以这样做(在字符串中使用换行符,这在 bash 中是可能的):
git filter-branch --env-filter '
if [ "$GIT_COMMITTER_NAME" = "<Old name>" ];
then
GIT_COMMITTER_NAME="<New name>";
GIT_COMMITTER_EMAIL="<New email>";
GIT_AUTHOR_NAME="<New name>";
GIT_AUTHOR_EMAIL="<New email>";
fi' -- --all
您可能会遇到以下错误之一:
临时目录已经存在 以 refs/original 开头的 Refs 已经存在(这意味着之前已经在存储库上运行了另一个过滤器分支,然后在 refs/original 处备份了原始分支引用)
如果您想在出现这些错误的情况下强制运行,请添加 --force
标志:
git filter-branch --force --env-filter '
if [ "$GIT_COMMITTER_NAME" = "<Old name>" ];
then
GIT_COMMITTER_NAME="<New name>";
GIT_COMMITTER_EMAIL="<New email>";
GIT_AUTHOR_NAME="<New name>";
GIT_AUTHOR_EMAIL="<New email>";
fi' -- --all
可能需要对 -- --all
选项进行一点解释:它使过滤器分支适用于 all refs 上的所有修订版(包括所有分支)。这意味着,例如,标签也被重写并且在重写的分支上可见。
一个常见的“错误”是改用 HEAD
,这意味着只过滤当前分支上的所有修订。然后在重写的分支中将不存在任何标签(或其他参考)。
git 文档 here 建议的 filter-repo
工具是 git filter-branch
更安全的替代品。
git filter-repo --commit-callback '
old_email = b"your-old-email@example.com"
correct_name = b"Your Correct Name"
correct_email = b"your-correct-email@example.com"
if commit.committer_email == old_email :
commit.committer_name = correct_name
commit.committer_email = correct_email
if commit.author_email == old_email :
commit.author_name = correct_name
commit.author_email = correct_email
'
上述命令反映了 this 脚本中使用的逻辑,但使用 filter-repo
而不是 filter-branch
。
commit-callback
选项之后的代码主体基本上是用于处理提交的 python 代码。你可以在这里用 python 编写你自己的逻辑。详细了解 commit
对象及其属性 here。
由于 filter-repo
工具未与 git 捆绑在一起,因此您需要单独安装它。
请参阅 Prerequisties 和 Installation Guide
如果你有一个 python env >= 3.5,你可以使用 pip
来安装它。
pip3 install git-filter-repo
注意:强烈建议在新克隆上试用 filter-repo
工具。操作完成后,遥控器也会被移除。详细了解为何移除遥控器here。另请阅读 INTERNALS 部分下此工具的限制。
运行 git rebase -i
这将保留所有其他提交信息(包括日期)。 --reuse-message=HEAD
选项阻止消息编辑器启动。
我使用以下内容重写整个存储库的作者,包括标签和所有分支:
git filter-branch --tag-name-filter cat --env-filter "
export GIT_AUTHOR_NAME='New name';
export GIT_AUTHOR_EMAIL='New email'
" -- --all
然后,如 MAN page of filter-branch 中所述,删除由 filter-branch
备份的所有原始 ref(这是破坏性的,先备份):
git for-each-ref --format="%(refname)" refs/original/ | \
xargs -n 1 git update-ref -d
--tag-name-filter cat
非常重要。否则,您的标签将保留在原始提交链上。其他答案没有提到这一点。
我调整了这个 solution,它通过摄取一个简单的 author-conv-file
(格式与 git-cvsimport 的格式相同)来工作。它通过更改所有分支的 author-conv-file
中定义的所有用户来工作。
我们将它与 cvs2git
结合使用,将我们的存储库从 cvs 迁移到 git。
即样本author-conv-file
john=John Doe <john.doe@hotmail.com>
jill=Jill Doe <jill.doe@hotmail.com>
剧本:
#!/bin/bash
export $authors_file=author-conv-file
git filter-branch -f --env-filter '
get_name () {
grep "^$1=" "$authors_file" |
sed "s/^.*=\(.*\) <.*>$/\1/"
}
get_email () {
grep "^$1=" "$authors_file" |
sed "s/^.*=.* <\(.*\)>$/\1/"
}
GIT_AUTHOR_NAME=$(get_name $GIT_COMMITTER_NAME) &&
GIT_AUTHOR_EMAIL=$(get_email $GIT_COMMITTER_NAME) &&
GIT_COMMITTER_NAME=$GIT_AUTHOR_NAME &&
GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL &&
export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL &&
export GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL
' -- --all
我发现提供的版本具有攻击性,特别是如果您提交其他开发人员的补丁,这实际上会窃取他们的代码。
下面的版本适用于所有分支,并分别更改作者和提交者以防止这种情况。
感谢 leif81 的所有选项。
#!/bin/bash
git filter-branch --env-filter '
if [ "$GIT_AUTHOR_NAME" = "<old author>" ];
then
GIT_AUTHOR_NAME="<new author>";
GIT_AUTHOR_EMAIL="<youmail@somehost.ext>";
fi
if [ "$GIT_COMMITTER_NAME" = "<old committer>" ];
then
GIT_COMMITTER_NAME="<new commiter>";
GIT_COMMITTER_EMAIL="<youmail@somehost.ext>";
fi
' -- --all
通过 Amend 更改提交作者姓名和电子邮件,然后将 old-commit 替换为 new-one: $ git checkout
最快、最简单的方法是使用 git rebase 的 --exec 参数:
git rebase -i -p --exec 'git commit --amend --reset-author --no-edit'
这将创建一个如下所示的待办事项列表:
pick ef11092 Blah blah blah
exec git commit --amend --reset-author --no-edit
pick 52d6391 Blah bloh bloo
exec git commit --amend --reset-author --no-edit
pick 30ebbfe Blah bluh bleh
exec git commit --amend --reset-author --no-edit
...
这将自动工作,当你有数百个提交时工作。
-p
替换为 --root
以更改历史记录中的 所有 次提交(不推荐使用 -p option)。请注意,这仅在您通过 git config user.name <yourname>
和 git config user.email <youremail>
更正用户名和电子邮件后才有效。
如果您是此存储库的唯一用户,您可以使用 git filter-branch
(如 svick wrote)或 git fast-export
/git fast-import
加上过滤器脚本(如参考文章中所述)重写历史记录在 docgnome answer 中)或交互式 rebase。但是其中任何一个都会从第一次更改提交开始更改修订;这对于任何将他/她的更改基于您的分支预重写的人来说都意味着麻烦。
恢复
如果其他开发人员的工作不是基于预重写版本,最简单的解决方案是重新克隆(再次克隆)。
或者,他们可以尝试 git rebase --pull
,如果他们的存储库中没有任何更改,这将快进,或者在重写的提交之上重新设置他们的分支(我们希望避免合并,因为它会保留重写前的提交永远)。所有这一切都假设他们没有提交工作;否则使用 git stash
隐藏更改。
如果其他开发人员使用功能分支和/或 git pull --rebase
不起作用,例如因为未设置上游,他们必须在重写后提交之上rebase他们的工作。例如,在获取新更改 (git fetch
) 之后,对于基于 / forked from origin/master
的 master
分支,需要运行
$ git rebase --onto origin/master origin/master@{1} master
这里 origin/master@{1}
是预重写状态(获取之前),请参见 gitrevisions。
另一种解决方案是使用 refs/replace/ 机制,从 1.6.5 版开始在 Git 中可用。在此解决方案中,您可以替换具有错误电子邮件的提交;那么任何获取“替换”引用的人(类似于 他们的 .git/config
中适当位置的 fetch = +refs/replace/*:refs/replace/*
引用规范)都会透明地获得替换,而那些不获取这些引用的人会看到旧的提交。
程序是这样的:
查找所有带有错误电子邮件的提交,例如使用 $ git log --author=user@wrong.email --all 对于每个错误的提交,创建一个替换提交,并将其添加到对象数据库中 $ git cat-file -p
你当然可以自动化这个过程......好吧,除了使用没有(还)批处理模式的 git replace
之外,你必须为此使用 shell 循环,或者替换“手动”。
未测试! YMMV。
请注意,您在使用 refs/replace/
机制时可能会遇到一些困难:它是新的,尚未经过很好的测试。
请注意,git 存储了两个不同的电子邮件地址,一个用于提交者(提交更改的人),另一个用于作者(编写更改的人)。
提交者信息不会在大多数地方显示,但您可以通过 git log -1 --format=%cn,%ce
看到它(或使用 show
而不是 log
来指定特定的提交)。
虽然更改上次提交的作者就像 git commit --amend --author "Author Name <email@example.com>"
一样简单,但没有单行或参数可以对提交者信息执行相同操作。
解决方案是(临时或不)更改您的用户信息,然后修改提交,这会将提交者更新为您当前的信息:
git config user.email my_other_email@example.com
git commit --amend
path\to\repo\.git
中的几个位置。我还不确定你需要做什么才能完全删除它。不幸的是,修正(?)似乎没有被抹去。
将所有提交(包括第一次提交)重置为当前用户和当前时间戳:
git rebase --root --exec "git commit --amend --no-edit --date 'now' --reset-author"
如果您要修复的提交是最新的,并且只是其中的几个,您可以使用 git reset
和 git stash
的组合在配置正确的名称和电子邮件后再次返回提交。
序列将是这样的(对于 2 次错误提交,没有待处理的更改):
git config user.name <good name>
git config user.email <good email>
git reset HEAD^
git stash
git reset HEAD^
git commit -a
git stash pop
git commit -a
如果您将 Eclipse 与 EGit 一起使用,那么有一个非常简单的解决方案。假设:您在本地分支“local_master_user_x”中有提交,由于用户无效,无法将其推送到远程分支“master”。
签出远程分支 'master' 选择 'local_master_user_x' 包含更改的项目/文件夹/文件右键单击 - 替换为 - 分支 - 'local_master_user_x' 再次提交这些更改,这次作为正确的用户并进入本地分支' master'推送到远程'master'
使用交互式变基,您可以在每次要更改的提交之后放置一个修改命令。例如:
pick a07cb86 Project tile template with full details and styling
x git commit --amend --reset-author -Chead
;-)
。
我们今天遇到了一个问题,作者姓名中的 UTF8 字符在构建服务器上造成了问题,因此我们不得不重写历史记录来纠正这个问题。采取的步骤是:
第 1 步:按照此处的说明,更改您在 git 中的用户名以进行所有未来提交:https://help.github.com/articles/setting-your-username-in-git/
第 2 步:运行以下 bash 脚本:
#!/bin/sh
REPO_URL=ssh://path/to/your.git
REPO_DIR=rewrite.tmp
# Clone the repository
git clone ${REPO_URL} ${REPO_DIR}
# Change to the cloned repository
cd ${REPO_DIR}
# Checkout all the remote branches as local tracking branches
git branch --list -r origin/* | cut -c10- | xargs -n1 git checkout
# Rewrite the history, use a system that will preseve the eol (or lack of in commit messages) - preferably Linux not OSX
git filter-branch --env-filter '
OLD_EMAIL="me@something.com"
CORRECT_NAME="New Me"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
fi
' --tag-name-filter cat -- --branches --tags
# Force push the rewritten branches + tags to the remote
git push -f
# Remove all knowledge that we did something
rm -rf ${REPO_DIR}
# Tell your colleagues to `git pull --rebase` on all their local remote tracking branches
快速概览:将存储库签出到临时文件,签出所有远程分支,运行将重写历史记录的脚本,强制推送新状态,并告诉所有同事执行 rebase pull 以获取更改。
我们在 OS X 上运行它时遇到了麻烦,因为它以某种方式弄乱了提交消息中的行尾,所以我们不得不在 Linux 机器上重新运行它。
你的问题真的很常见。请参阅“Using Mailmap to Fix Authors List in Git”
为简单起见,我创建了一个脚本来简化该过程:git-changemail
将该脚本放在您的路径上后,您可以发出如下命令:
更改当前分支上的作者匹配 $ git changemail -a old@email.com -n newname -m new@email.com
更改
在 repo 上显示现有用户 $ git changemail --show-both
顺便说一句,在进行更改后,使用以下命令清除过滤器分支中的备份:git-backup-clean
chmod +x git-changemail
设置执行权限
如果您想(轻松)更改当前分支的作者,我将使用如下内容:
# update author for everything since origin/master
git rebase \
-i origin/master \
--exec 'git commit --amend --no-edit --author="Author Name <author.name@email.co.uk>"'
不定期副业成功案例分享
user.name
和user.email
后,git commit --amend --reset-author
也可以工作。~/.gitconfig
中的user.name
和user.email
重写<commit>
之后所有提交的作者信息:运行git rebase -i <commit> --exec 'git commit --amend --reset-author --no-edit'
,保存,退出。无需编辑!