GitのコミットログにはAuthorとCommitterが記録されます。Authorはオリジナルのコードを書いた人、Committerはコミットした人です。例えば下記のようになります。
$ git show --pretty=fuller | head -n5 commit b39f4146de84d7b36861859ec669d9c8e2ca77c6 Author: Richard Purdie richard.purdie@linuxfoundation.org AuthorDate: Wed Jul 8 12:07:42 2020 +0100 Commit: Richard Purdie richard.purdie@linuxfoundation.org CommitDate: Tue Sep 8 14:46:17 2020 +0100
自分でコード書いて自分でコミットする場合は上記のようにどちらも同じ人になりますが、他の人がrebaseしたりするとAuthorとCommitterが別の人になったりします。
このAuthorやCommitterは基本的に手動で設定したりすることはないのですが、ごく稀にあとから変更したくなる場合などもあります。原則としてそのような無法なことはしてはいけませんが、時と場合によっては必要になることもあるかもしれません。今回はその方法を説明します。Committer/Author の名前やメールアドレス、また時刻を修正します。
コミッターおよびコミット時刻の修正
コミッターの修正はAuthorに比べると簡単です。GIT_COMMITTER_NAME、GIT_COMMITTER_EMAIL、GIT_COMMITTER_DATE変数を利用すればOKです。下記の例では YAMADA TAROさんがコミットしたということに修正しています。
時刻のタイムゾーンが JST +0900 ではなく +0100 になっていますが深い意味はありません。
$ git show --pretty=fuller | head -n5 commit b39f4146de84d7b36861859ec669d9c8e2ca77c6 Author: Richard Purdie richard.purdie@linuxfoundation.org AuthorDate: Wed Jul 8 12:07:42 2020 +0100 Commit: Richard Purdie richard.purdie@linuxfoundation.org CommitDate: Tue Sep 8 14:46:17 2020 +0100 $ GIT_COMMITTER_DATE="2025-08-03 12:11:13+01:00" GIT_COMMITTER_NAME="YAMADA TARO" GIT_COMMITTER_EMAIL="hogehoge@hoge.com" git commit --amend --no-edit [detached HEAD c9fe1f9c87] bitbake: fetch2: Change git fetcher not to destroy old references Author: Richard Purdie Date: Wed Jul 8 12:07:42 2020 +0100 1 file changed, 1 insertion(+), 1 deletion(-) $ git show --pretty=fuller | head -n5 commit c9fe1f9c871ec4ef00906f9486901140c86bbeba Author: Richard Purdie AuthorDate: Wed Jul 8 12:07:42 2020 +0100 Commit: YAMADA TARO CommitDate: Sun Aug 3 12:11:13 2025 +0100
Authorおよびコミット時刻の修正
Authorは最初に述べたようにオリジナルのコード作成者です。なので簡単には変更できません。そこで下記のgit filter-repoを使用します。
このツールには注意点があるので先にその点を述べておきます。
このツールを実行すると下記のような警告がでます。リモートのoriginを削除してしまいます。これは歴史改変したコミットを意図せずpushしないようにするための措置でしょう。
NOTICE: Removing 'origin' remote; see 'Why is my origin removed?' in the manual if you want to push back there. (was https://github.com/your-username/your-repository.git)
下記のような感じで再度リモートを追加すればよいでしょう。
$ git add remote orign https://github.com/your-username/your-repository.git
さて、ツールの使い方に戻ります。
まずはwgetでpythonのコードをダウンロードします。
$ wget https://raw.githubusercontent.com/newren/git-filter-repo/refs/heads/main/git-filter-repo
使い方は下記のとおりです。注意点は、時刻指定はUNIX時間(エポック秒)で指定する必要があります。下記ではdateコマンドでUnix時間を取得しています。
$ python3 git-filter-repo --commit-callback "commit.author_name = b'YAMADA TARO'; commit.author_email = b'hogehoge@hoge.com'; commit.author_date = b'$(date +%s) $(date +%z)'" --force $ git show --pretty=fuller | head -n5 commit c68372c4aefd3f66b692b586f3f4af04c4eb0763 Author: YAMADA TARO [hogehoge@hogecom](mailto:hogehoge@hogecom) AuthorDate: Thu Aug 7 12:11:13 2025 +0900 Commit: YAMADA TARO [hogehoge@hoge.com](mailto:hogehoge@hoge.com) CommitDate: Sun Aug 3 12:11:13 2025 +0100
これでAuthorも歴史改変できました。
まとめ
Gitの禁じ手である、Committer/Author およびその時刻の改変の方法を説明しました。めったに使うことはないですが、使う際は慎重に実施しましょう。
コメント