题
解决方案
从历史记录中比您需要修改的提交更早的点进行交互式变基(git rebase -i <earliercommit>
)。在正在重新设定基准的提交列表中,将文本更改为 pick
到 edit
在您要修改的哈希值旁边。然后当 git 提示您更改提交时,请使用以下命令:
git commit --amend --author="Author Name <email@address.com>"
例如,如果您的提交历史记录是 A-B-C-D-E-F
和 F
作为 HEAD
, ,并且您想更改作者 C
和 D
, ,那么你会...
- 指定
git rebase -i B
(这是执行后您将看到的示例git rebase -i B
命令)- 如果您需要编辑
A
, , 使用git rebase -i --root
- 如果您需要编辑
- 更改两者的行
C
和D
从pick
到edit
- 一旦变基开始,它首先会暂停在
C
- 你会
git commit --amend --author="Author Name <email@address.com>"
- 然后
git rebase --continue
- 它会再次暂停在
D
- 那么你会
git commit --amend --author="Author Name <email@address.com>"
再次 git rebase --continue
- 变基将完成。
- 使用
git push -f
使用更新的提交更新您的源。
其他提示
这 接受的答案 对于这个问题,交互式 rebase 的使用非常巧妙,但不幸的是,如果我们试图更改作者的提交曾经位于随后合并的分支上,那么它会出现冲突。更一般地说,它在处理混乱的历史记录时不起作用。
由于我担心运行依赖于设置和取消设置环境变量来重写 git 历史记录的脚本,因此我正在根据以下内容编写一个新答案 这个帖子 这类似于 这个答案 但比较完整。
与链接的答案不同,以下内容经过测试并有效。为了说明清楚起见,假设 03f482d6
是我们试图替换其作者的提交,并且 42627abe
是新作者的提交。
检查我们正在尝试修改的提交。
git checkout 03f482d6
让作者改变。
git commit --amend --author "New Author Name <New Author Email>"
现在我们有了一个新的提交,其哈希值假设为
42627abe
.查看原来的分支。
在本地将旧提交替换为新提交。
git replace 03f482d6 42627abe
根据替换重写所有未来的提交。
git filter-branch -- --all
取下替换件以保持清洁。
git replace -d 03f482d6
推送新的历史记录(仅当以下失败时才使用 --force,并且仅在使用
git log
和/或git diff
).git push --force-with-lease
您可以将 4-6 重新设置为新的提交,而不是 4-6:
git rebase -i 42627abe
Github的文档包含一个脚本,取代了在一个分支的所有提交的提交者信息
#!/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 config --global user.email example@email.com
现在重置提交的作者,无需编辑:
git commit --amend --reset-author --no-edit
可以改变的最后提交使用下面的命令提交。
git commit --amend --author="Author Name <email@address.com>"
不过,如果你想改变一个以上的提交作者姓名,这是一个有点棘手。你需要启动一个交互式的rebase标记,然后提交的修改,然后通过一个和完成ammend其中之一。
开始与git rebase -i
衍合。它会告诉你这样的事情。
更改pick
关键字edit
您要更改作者名的提交。
然后关闭编辑器。对于初学者,命中Escape
然后键入:wq
和命中Enter
。
然后你会看到你的终端就像什么都没有发生。其实你是在互动变基的中间。现在是时候修改你使用上面的命令提交的作者姓名。这将再次打开编辑器。退出并继续与底垫git rebase --continue
。重复同样的承诺算你想编辑。可以肯定的是作出这样的,当你得到No rebase in progress?
消息互动变基完成。
为您链接的问题的答案是很好的答案,涵盖您的情况(另一问题是更普遍的,因为它涉及多个重写提交)。
为借口尝试git filter-branch
,我写了一个剧本重写作者名称和/或作者电邮地址给定一个承诺:
#!/bin/sh
#
# Change the author name and/or email of a single commit.
#
# change-author [-f] commit-to-change [branch-to-rewrite [new-name [new-email]]]
#
# If -f is supplied it is passed to "git filter-branch".
#
# If <branch-to-rewrite> is not provided or is empty HEAD will be used.
# Use "--all" or a space separated list (e.g. "master next") to rewrite
# multiple branches.
#
# If <new-name> (or <new-email>) is not provided or is empty, the normal
# user.name (user.email) Git configuration value will be used.
#
force=''
if test "x$1" = "x-f"; then
force='-f'
shift
fi
die() {
printf '%s\n' "$@"
exit 128
}
targ="$(git rev-parse --verify "$1" 2>/dev/null)" || die "$1 is not a commit"
br="${2:-HEAD}"
TARG_COMMIT="$targ"
TARG_NAME="${3-}"
TARG_EMAIL="${4-}"
export TARG_COMMIT TARG_NAME TARG_EMAIL
filt='
if test "$GIT_COMMIT" = "$TARG_COMMIT"; then
if test -n "$TARG_EMAIL"; then
GIT_AUTHOR_EMAIL="$TARG_EMAIL"
export GIT_AUTHOR_EMAIL
else
unset GIT_AUTHOR_EMAIL
fi
if test -n "$TARG_NAME"; then
GIT_AUTHOR_NAME="$TARG_NAME"
export GIT_AUTHOR_NAME
else
unset GIT_AUTHOR_NAME
fi
fi
'
git filter-branch $force --env-filter "$filt" -- $br
提交之前:
要修复作者所有提交你可以从@琥珀的回答适用的命令:
git commit --amend --author="Author Name <email@address.com>"
,或重复使用您的姓名和邮件,你可以这样写:
git commit --amend --author=Eugen
命令后提交:
例如,以改变从4025621
所有的起始
您必须运行:
git rebase --onto 4025621 --exec "git commit --amend --author=Eugen" 4025621
注意:要包括含有空格,如姓名和电子邮件地址的作者,作者必须由转义的引号括起来。例如:
git rebase --onto 4025621 --exec "git commit --amend --author=\"Foo Bar <foo@bar.com>\"" 4025621
或添加此别名到~/.gitconfig
:
[alias]
reauthor = !bash -c 'git rebase --onto $1 --exec \"git commit --amend --author=$2\" $1' --
和然后运行:
git reauthor 4025621 Eugen
做的时候 git rebase -i
文档中有一个有趣的部分:
如果要将两个或多个提交合并为一个,请替换命令
"pick"
对于第二次和后续提交"squash"
或者"fixup"
. 。如果提交有不同的作者,则折叠的提交将归因于第一个提交的作者。折叠提交的建议提交消息是第一个提交的提交消息和带有第一个提交的提交消息的串联。"squash"
命令,但省略了提交的提交消息"fixup"
命令。
- 如果您有以下病史
A-B-C-D-E-F
, - 并且你想更改提交
B
和D
(= 2 次提交),
那么你可以这样做:
git config user.name "Correct new name"
git config user.email "correct@new.email"
- 创建空提交(每个提交一个):
- 您需要一条消息用于变基目的
git commit --allow-empty -m "empty"
- 启动变基操作
git rebase -i B^
B^
选择的父级B
.
- 你会想要放置一个空提交 前 每次提交修改
- 你会想要改变
pick
到squash
对于那些。
举例说明什么 git rebase -i B^
会给你:
pick sha-commit-B some message
pick sha-commit-C some message
pick sha-commit-D some message
pick sha-commit-E some message
pick sha-commit-F some message
# pick sha-commit-empty1 empty
# pick sha-commit-empty2 empty
将其更改为:
# change commit B's author
pick sha-commit-empty1 empty
squash sha-commit-B some message
# leave commit C alone
pick sha-commit-C some message
# change commit D's author
pick sha-commit-empty2 empty
squash sha-commit-D some message
# leave commit E-F alone
pick sha-commit-E some message
pick sha-commit-F some message
它将提示您编辑消息:
# This is a combination of 2 commits.
# The first commit's message is:
empty
# This is the 2nd commit message:
...some useful commit message there...
你可以删除前几行。
在赞助到尤金Konkov 响应,以启动从根提交,使用--root
标志。所述--no-edit
标志是有用的太
git rebase --root --exec "git commit --amend --author='name <email>' --no-edit"
如果你需要改变什么的作者最后一次提交,并没有其他正在使用您的信息库,则可以撤销上次提交有:
git push -f origin last_commit_hash:branch_name
更改的作者姓名与提交:
git commit --amend --author "type new author here"
退出打开的编辑器,并再次按您的代码:
git push
还有一个偷懒的办法解决这个问题,特别是如果你有一个以上的承诺,你想改变。就我而言,我有多次提交了错误的作者一个新的分支,所以有什么帮助我:
转到原来的分支:
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的结帐branch_name 强>
查找提交提交之前要改变,找到它的哈希值。然后问题变基命令。
<强> GIT中底垫-i -p提交强>
的哈希然后编辑器会打开并进入“修改”。要改变的提交。让别人使用默认“挑”选项。一旦改变输入“ESC”键和WQ!退出。
然后发出git的承诺与修正选项命令。
<强> git的承诺--amend --author = “用户名的电子邮件” --no-修改
然后发出以下命令。
<强> GIT中底垫--continue 强>
在提交作者在本地资源库中更新,按更改到远程存储库。
提交提交后重命名作者姓名的步骤
- 首先输入“git log”以获取提交 ID 和更多详细信息
git rebase i HEAD~10 (10 是在 rebase 上显示的总提交)
If you Get anything like below
fatal: It seems that there is already a rebase-merge directory, and I wonder if you are in the middle of another rebase. If that is the case, please try
git rebase (--continue | --abort | --skip)
If that is not the case, please rm -fr ".git/rebase-merge" and run me again. I am stopping in case you still have something valuable there.
然后根据您的需要输入“git rebase --continue”或“git rebase --abort”
- 现在您的意志变基窗口打开,单击键盘上的“i”键
- 然后,您将获得10的提交列表[因为我们已经通过了10个提交]如下
pick 897fe9e simplify code a little
pick abb60f9 add new feature
pick dc18f70 bugfix
现在您需要在要编辑的提交下方添加以下命令,如下所示
pick 897fe9e simplify code a little exec git commit --amend --author 'Author Name <author.name@mail.com>' pick abb60f9 add new feature exec git commit --amend --author 'Author Name <author.name@mail.com>' pick dc18f70 bugfix exec git commit --amend --author 'Author Name <author.name@mail.com>'
就是这样,现在只需按 ESC, :wq 就可以了
然后 git push origin HEAD:BRANCH NAME -f [请注意 -f 强制推送]
喜欢
git push -f
或者git push origin HEAD: dev -f