我想改变历史上某个特定承诺的作者。这不是最新的承诺。
相关:如何更改多次提交的作者和提交人姓名/电子邮件?
我想改变历史上某个特定承诺的作者。这不是最新的承诺。
相关:如何更改多次提交的作者和提交人姓名/电子邮件?
当前回答
找到一种可以快速改变用户并且不会对其他提交产生副作用的方法。
简单明了的方式:
git config user.name "New User"
git config user.email "newuser@gmail.com"
git log
git rebase -i 1f1357
# change the word 'pick' to 'edit', save and exit
git commit --amend --reset-author --no-edit
git rebase --continue
git push --force-with-lease
详细操作
显示提交日志,并在提交之前找出要更改的提交id:
git log
git从选择的提交id反向开始到最近的提交id:
git config user.name "New User"
git config user.email "newuser@gmail.com"
git rebase -i 1f1357
# change word pick to edit, save and exit
edit 809b8f7 change code order
pick 9baaae5 add prometheus monitor kubernetes
edit 5d726c3 fix liquid escape issue
edit 3a5f98f update tags
pick 816e21c add prometheus monitor kubernetes
rebase将在下一次提交id时停止,输出:
Stopped at 809b8f7... change code order
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue
确认并继续您的重新基准,直到它成功地传递给refs/heads/master。
# each continue will show you an amend message
# use git commit --amend --reset-author --no-edit to comfirm
# use git rebase --skip to skip
git commit --amend --reset-author --no-edit
git rebase --continue
git commit --amend --reset-author --no-edit
...
git rebase --continue
Successfully rebased and updated refs/heads/master.
git推送更新
git push --force-with-lease
其他回答
对于这个问题,也有一种懒惰的方法,特别是当您有多个提交需要更改时。在我的案例中,我有一个新的分支,它与错误的作者进行了多次提交,所以是什么帮助了我:
转到您的原始分支:
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
之后,您可以删除具有错误作者的分支。
您可以使用下面的命令更改上次提交的作者。
gitcommit--modify--author=“作者名称<email@address.com>"
然而,如果您想更改多个提交的作者名称,这有点棘手。您需要启动一个交互式的rebase,然后将提交标记为edit,然后逐一修改并完成。
使用git rebase-i开始重新定基。它会向你展示这样的东西。
更改pick关键字以编辑要更改作者名称的提交。
然后关闭编辑器。对于初学者,点击Escape,然后键入:wq并点击Enter。
然后你会看到你的终端就像什么都没发生一样。事实上,您正在进行交互式重设基。现在是使用上面的命令修改提交的作者名称的时候了。它将再次打开编辑器。退出并继续使用git rebase重新创建数据库--继续。对要编辑的提交计数重复相同的操作。您可以确保在执行No rebase时完成交互式rebase?消息
如果您的机器上缺少设置,例如,在格式化之后,或者在没有正确设置(正确)这些命令的情况下没有正确配置Git,则可能会发生这种情况。
git config user.name "Author Name"
git config user.email "<email@address.com>"
为什么不遵循Atlassian的这篇文章,让你的生活更简单?
gitcommit--modify--author=“作者名称<email@address.com>"如果分支受到保护,请取消对其的保护。在本例中,它是master;因此,它将受到源代码存储库的保护git推送原点master—力
这是最后一次提交的最简单场景。要选择任何“随机”提交,您需要:
git rebase-i<早期提交>。更改您感兴趣的提交的编辑选择gitcommit--modify--author=“作者名称<email@address.com>"如果分支受到保护,请取消对其的保护。在本例中,它是master;因此,它将受到源代码存储库的保护git推送原点master—力
在推送之前,您可以随时在两者之间进行git登录,以确定您所在的位置。
你所链接的问题中的答案是好答案,涵盖了你的情况(另一个问题更为一般,因为它涉及重写多次提交)。
作为尝试gitfilter分支的借口,我编写了一个脚本来重写给定提交的作者名和/或作者电子邮件:
#!/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 config user.name "New User"
git config user.email "newuser@gmail.com"
git log
git rebase -i 1f1357
# change the word 'pick' to 'edit', save and exit
git commit --amend --reset-author --no-edit
git rebase --continue
git push --force-with-lease
详细操作
显示提交日志,并在提交之前找出要更改的提交id:
git log
git从选择的提交id反向开始到最近的提交id:
git config user.name "New User"
git config user.email "newuser@gmail.com"
git rebase -i 1f1357
# change word pick to edit, save and exit
edit 809b8f7 change code order
pick 9baaae5 add prometheus monitor kubernetes
edit 5d726c3 fix liquid escape issue
edit 3a5f98f update tags
pick 816e21c add prometheus monitor kubernetes
rebase将在下一次提交id时停止,输出:
Stopped at 809b8f7... change code order
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue
确认并继续您的重新基准,直到它成功地传递给refs/heads/master。
# each continue will show you an amend message
# use git commit --amend --reset-author --no-edit to comfirm
# use git rebase --skip to skip
git commit --amend --reset-author --no-edit
git rebase --continue
git commit --amend --reset-author --no-edit
...
git rebase --continue
Successfully rebased and updated refs/heads/master.
git推送更新
git push --force-with-lease