履歴の中の特定のコミットの作者を変更したいです。それは最後のコミットではありません。
この質問については知っています - [How do I change the author of a commit in git?
しかし、私が考えているのは、ハッシュやショートハッシュでコミットを特定する方法です。
インタラクティブなリベースは、変更したいコミットよりも履歴の前の時点から行います(git rebase -i <earliercommit>
)。リベースされるコミットの一覧で、修正したいコミットのハッシュの横にあるテキストを pick
から edit
に変更します。そして git がコミットの変更を促すプロンプトを表示したら、次のようにします。
git commit --amend --author="Author Name <[email protected]>"
例えば、コミット履歴が A-B-C-D-E-F
で F
が HEAD
となっていて、C
と D
の作者を変更したい場合、次のようにします。
1.git rebase -i Bを指定する ([ここでは、
git rebase -i B` コマンドを実行した後に表示される例を示します](https://help.github.com/articles/about-git-rebase/#an-example-of-using-git-rebase))。
A
を編集する必要がある場合は、git rebase -i --root
を使用してください。C
と D
の両方の行を pick
から edit
に変更してください。
3.リベースが始まると、まず C
で一時停止します。
4.git commit --amend --author="Author Name [email protected]"を実行します。 5.次に
git rebase --continueを実行します。 6.再び
Dで一時停止します。 7.その後、再び
git commit --amend --author="Author Name [email protected]"を実行します。 8.8.
git rebase --continueを実行します。 9.リベースが完了します。 10.10.
git push -f` を使って、更新されたコミットで自分のオリジンを更新します。リンク先の質問の答えは良い答えで、あなたの状況をカバーしています(もう一つの質問は、複数のコミットを書き換えることになるので、より一般的な質問です)。
git filter-branch` を試す口実として、あるコミットの Author Name や Author Email を書き換えるスクリプトを書いてみました。
#!/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 rebase -i` を実行する際に、ドキュメントには次のような興味深い記述があります。
2つ以上のコミットを1つにまとめたい場合は、2つ目以降のコミットの
"pick"
コマンドを"squash"
または"fixup"
に置き換えてください。コミットの作成者が異なる場合、折りたたまれたコミットは最初のコミットの作成者に帰属します。折りたたまれたコミットのコミットメッセージの候補は、最初のコミットと"squash"
コマンドを使用したコミットのコミットメッセージを結合したもので、"fixup"
コマンドを使用したコミットのコミットメッセージは含まれていません。
.という履歴があって、
"squash"`コマンドでコミットを変更したい場合。B
と D
(=2つのコミット)を変更したいとします。とすれば、次のようになります。
git config user.name "Correct new name"
git config user.email "[email protected]"
git commit --allow-empty -m "empty"
git rebase -i 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...
と表示され、最初の数行を削除することができます。