#!/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"; thenforce='-f'shiftfi
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"; thenif test -n "$TARG_EMAIL"; thenGIT_AUTHOR_EMAIL="$TARG_EMAIL"export GIT_AUTHOR_EMAILelseunset GIT_AUTHOR_EMAILfiif test -n "$TARG_NAME"; thenGIT_AUTHOR_NAME="$TARG_NAME"export GIT_AUTHOR_NAMEelseunset GIT_AUTHOR_NAMEfifi
'
git filter-branch $force --env-filter "$filt" -- $br
pick sha-commit-B some messagepick sha-commit-C some messagepick sha-commit-D some messagepick sha-commit-E some messagepick sha-commit-F some message# pick sha-commit-empty1 empty# pick sha-commit-empty2 empty
将其更改为:
# change commit B's authorpick sha-commit-empty1 emptysquash sha-commit-B some message# leave commit C alonepick sha-commit-C some message# change commit D's authorpick sha-commit-empty2 emptysquash sha-commit-D some message# leave commit E-F alonepick sha-commit-E some messagepick 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...
然后你会看到你的终端,就像什么都没发生一样。实际上你正处于交互式rebase的中间。现在是时候使用上面的命令修改提交的作者名了。它将再次打开编辑器。退出并继续使用git rebase --continue进行rebase。对要编辑的提交计数重复相同的操作。您可以确保在收到No rebase in progress?消息时交互式rebase完成。
git config user.name "New User"git config user.email "newuser@gmail.com"
git loggit rebase -i 1f1357# change the word 'pick' to 'edit', save and exit
git commit --amend --reset-author --no-editgit rebase --continue
git push --force-with-lease
详细操作
显示提交日志并找出您要更改的提交之前的提交ID:
git log
git rebase从选择的提交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 exitedit 809b8f7 change code orderpick 9baaae5 add prometheus monitor kubernetesedit 5d726c3 fix liquid escape issueedit 3a5f98f update tagspick 816e21c add prometheus monitor kubernetes
rebase将在下一次提交id时停止,输出:
Stopped at 809b8f7... change code orderYou can amend the commit now, withgit commit --amend
Once you are satisfied with your changes, run
git rebase --continue
确认并继续您的rebase,直到它成功地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 skipgit commit --amend --reset-author --no-editgit rebase --continuegit commit --amend --reset-author --no-edit...git rebase --continueSuccessfully rebased and updated refs/heads/master.
$ git log // Old author in local and remote$ git commit --amend --author="Author Name <email@address.com>"$ git log // New Author in local$ git push origin <branch> --force-with-lease$ git log // New Author in remote
在打开的编辑器中,在要编辑的每个提交行之后添加一行并添加exec git commit --amend --author="Author Name <email@address.com>" --no-edit(如果要重置为git config中设置的值,则使用--reset-author)
保存并退出-这将为每个提交运行指定的命令,有效地更改作者
示例编辑器内容(更改前2个提交作者):
pick 1fc6c95 Patch Aexec git commit --amend --author="Author Name <email@address.com>" --no-editpick 6b2481b Patch Bexec git commit --amend --author="Author Name <email@address.com>" --no-editpick dd1475d something I want to splitpick c619268 A fix for Patch Bpick fa39187 something to add to patch Apick 4ca2acc i cant' typ goodspick 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.#