#!/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
6
将您的电子邮件重置为全局配置:
git config --global user.email example@email.com
现在重置您的提交作者,无需编辑:
git commit --amend --reset-author --no-edit
12
承诺之前:
要修复所有提交的作者,您可以从@ Amber的答案中应用命令:
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
然后你会看到你的终端没有发生任何事情 . 实际上,你正处于一个互动的rebase中间 . 现在它使用上面的命令's time to amend your commit'作者姓名 . 它将再次打开编辑器 . 退出并继续使用 git rebase --continue 进行rebase . 对要编辑的提交计数重复相同的操作 . 收到 No rebase in progress? 消息后,您可以确保交互式rebase已完成 .
9 回答
您链接的问题中的答案是很好的答案并涵盖您的情况(另一个问题是更一般的,因为它涉及重写多个提交) .
作为尝试
git filter-branch
的借口,我编写了一个脚本来重写给定提交的作者姓名和/或作者电子邮件:git config --global user.email example@email.com
git commit --amend --reset-author --no-edit
承诺之前:
要修复所有提交的作者,您可以从@ Amber的答案中应用命令:
或者重复使用您的姓名和电子邮件,您可以写:
命令后提交:
例如,要从
4025621
开始更改所有内容:你必须运行:
注意:要包含包含名称和电子邮件地址等空格的作者,作者必须被转义引号括起来 . 例如:
或者将此别名添加到
~/.gitconfig
中:然后运行:
交互式rebase关闭历史记录中早于您需要修改的提交(
git rebase -i <earliercommit>
) . 在要重新提交的提交列表中,将文本从pick
更改为要修改的哈希旁边的edit
. 然后,当git提示您更改提交时,请使用以下命令:例如,如果您的提交历史记录为
A-B-C-D-E-F
且F
为HEAD
,并且您想要更改C
和D
的作者,那么您会......指定
git rebase -i B
(here is an example of what you will see after executing the git rebase -i B command)如果您需要编辑
A
,请使用git rebase -i --root
将
C
和D
的行从pick
更改为edit
一旦rebase开始,它将首先暂停
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
rebase将完成 .
使用
git push -f
使用更新的提交更新您的来源 .这个问题的accepted answer非常巧妙地使用了交互式rebase,但不幸的是,如果我们试图改变以前的作者的提交是在一个随后被合并的分支上,那么它就会出现冲突 . 更一般地说,它不起作用处理凌乱的历史 .
由于我担心运行依赖于设置和取消设置环境变量的脚本来重写git历史记录,我正在编写一个基于this post的新答案,它类似于this answer但更完整 .
与链接的答案不同,以下内容经过测试和运行 . 为了清楚地说明
03f482d6
是我们正在尝试替换的作者的提交,并且42627abe
是新作者的提交 .现在我们有一个新的提交,哈希假定为
42627abe
.签出原始分支 .
用本地新提交替换旧提交 .
git log
和/或git diff
进行完整性检查后) .而不是4-6你可以改变到新的提交:
如果您使用的是集中式存储库,那么Amber's answer还有一个额外的步骤:
git push -f
强制更新中央存储库 .要小心,没有很多人在同一个分支上工作,因为它会破坏一致性 .
Github文档包含a script that replaces the committer info for all commits in a branch .
在做
git rebase -i
时,文档中有一个有趣的内容:如果你有
A-B-C-D-E-F
的历史,并且您想要更改提交
B
和D
(= 2次提交),然后你可以这样做:
git config user.name "Correct new name"
git config user.email "correct@new.email"
创建空提交(每次提交一次):
你需要一条消息用于rebase目的
git commit --allow-empty -m "empty"
启动rebase操作
git rebase -i B^
B^
选择B
的父级 .你想要将每个提交一个空提交 before 修改
您需要将
pick
更改为squash
.git rebase -i B^
将为您提供的示例:改为:
它会提示您编辑消息:
你可以删除前几行 .
您可以使用以下命令更改上次提交的作者 .
git commit --amend --author="Author Name <email@address.com>"
但是,如果要更改多个提交作者名称,则有点棘手 . 您需要启动交互式rebase,然后将提交标记为编辑,然后逐个修改它们并完成 .
使用
git rebase -i
开始变基 . 它会告诉你这样的事情 .对于要更改作者姓名的提交,请将
pick
关键字更改为edit
.然后关闭编辑器 . 对于初学者,点击
Escape
然后输入:wq
并点击Enter
.然后你会看到你的终端没有发生任何事情 . 实际上,你正处于一个互动的rebase中间 . 现在它使用上面的命令's time to amend your commit'作者姓名 . 它将再次打开编辑器 . 退出并继续使用
git rebase --continue
进行rebase . 对要编辑的提交计数重复相同的操作 . 收到No rebase in progress?
消息后,您可以确保交互式rebase已完成 .