我想改变历史上某个特定承诺的作者。这不是最新的承诺。

相关:如何更改多次提交的作者和提交人姓名/电子邮件?


当前回答

对于这个问题,也有一种懒惰的方法,特别是当您有多个提交需要更改时。在我的案例中,我有一个新的分支,它与错误的作者进行了多次提交,所以是什么帮助了我:

转到您的原始分支:

git checkout develop

从中创建新分支:

git checkout -b myFeature develop 

将其合并为一次提交,但不包含提交信息:

git merge --no-commit --squash branchWrongAuthor

您可能还想进行更改:

git stage .

更改作者姓名并提交更改:

git commit --amend --author "New Author Name <New Author Email>" -m "new feature added"

就这样,你可以推动改变。

git push

之后,您可以删除具有错误作者的分支。

其他回答

使用交互式回扣

git rebase -i -p <some HEAD before all of your bad commits>

然后在rebase文件中将所有错误提交标记为“edit”,当git要求您修改每个提交时

git commit --amend --author "New Author Name <email@address.com>"

编辑或关闭打开的编辑器,然后执行

git rebase --continue

以继续重新启动。

您可以通过附加--no edit来跳过在此处完全打开编辑器,这样命令将是:

git commit --amend --author "New Author Name <email@address.com>" --no-edit && \
git rebase --continue

单次提交

正如一些评论所指出的,如果您只想更改最近的提交,那么rebase命令是不必要的。就这样吧

git commit --amend --author "New Author Name <email@address.com>"

这会将author更改为指定的名称,但committer将设置为gitconfig user.name和gitconfig user.email中配置的用户。如果要将committer设置为指定的值,这将同时设置author和committer:

git -c user.name="New Author Name" -c user.email=email@address.com commit --amend --reset-author

如果您只想更改上次提交的作者,可以执行以下操作:

将电子邮件重置为全局配置:git-config--全局用户电子邮件example@email.com现在重置提交的作者,无需编辑:gitcommit--修改--重置作者--无编辑

注意,这也会更改作者时间戳。

如果您使用的是集中存储库,Amber的答案还有一个步骤:

gitpush-f强制更新中央存储库。

请注意,在同一分支上工作的人不多,因为这会破坏一致性。

有一个快捷方式适用于投票最多的问题:使用exec而不是edit。

exec允许对指定的提交运行命令。使用它可以避免使用edit、退出终端并为每个git提交运行git命令。如果您必须更改历史记录中的多次提交,这尤其有用。

步骤如下:

执行对早期提交的重新基础(gitrebase-i<earliercommit>)在打开的编辑器中,在要编辑的每个提交行后添加一行,然后添加exec git commit--modify--author=“作者名称<email@address.com>“--不编辑(如果要重置为gitconfig中设置的值,请使用--reset-author)”保存并退出-这将为每次提交运行指定的命令,有效地更改作者

编辑器内容示例(更改前两个提交作者):

pick 1fc6c95 Patch A
exec git commit --amend --author="Author Name <email@address.com>" --no-edit
pick 6b2481b Patch B
exec git commit --amend --author="Author Name <email@address.com>" --no-edit
pick dd1475d something I want to split
pick c619268 A fix for Patch B
pick fa39187 something to add to patch A
pick 4ca2acc i cant' typ goods
pick 7b36971 something to move before patch B

# Rebase 41a72e6..7b36971 onto 41a72e6
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

您可以从github的官方页面使用这些命令

https://help.github.com/en/github/using-git/changing-author-info

这是命令

#!/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

在这里,您可以将旧电子邮件更改为新用户名和电子邮件地址。