0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Githubのコミット履歴を一括で書き換える方法

Last updated at Posted at 2025-08-24

GitHubでは、過去のコミット履歴に残っているユーザー名やメールアドレスを後から修正することができます。
本記事では、誤って実際のメールアドレスを公開してしまった場合などに対応する方法を簡単にまとめます。

メールアドレスの非公開について

GitHubの設定でメールアドレスを非公開にすることができます。

  • Settings -> Emails -> Keep my email addresses privateを有効化

この設定をすると、GitHubはコミットに使うメールアドレスとして
xxxxxxx@users.noreply.github.com を割り当てます。

git config

ローカルPCなどのgit configに実際のメールアドレスを設定してしまうと、メールアドレス非公開とは関係なく、そのままコミットに残り、公開リポジトリにpushした時点で誰でも閲覧できる状態になります。

git configに設定するメールアドレスは自由に指定できますが、GitHubに登録済みのメールアドレス、または @users.noreply.github.comを使わないと、プロフィール上のContributions(通称草)に反映されない場合があります

設定を修正するには以下のコマンドを実行します。

git config --global user.name "sampleUser"
git config --global user.email "12345678+sampleUser@users.noreply.github.com"

既存コミット情報の更新

git-filter-repo

既にpushしてしまったコミットも、git filter-repoを使って書き換えることができます。

macOSの場合はbrewでインストールできます。

brew install git-filter-repo

mailmapファイルの作成

書き換えルールを定義するためのmailmap.txtを作成します。

vi ~/mailmap.txt

今回は古いアドレスを新しいアドレスに置き換えます。
複数の変換をしたい場合は改行して追加できます。

mailmap.txt
<newname@users.noreply.github.com> <oldname@example.com>

対象リポジトリでコマンド実行

まず、現在のコミット履歴に含まれるメールアドレスを確認します。

cd <対象リポジトリ>
git log --all --pretty=format:"%an <%ae>" | sort -u
sampleUser <oldname@example.com>

git filter-repoコマンドを実行して、履歴を書き換えます。

git filter-repo --mailmap ~/mailmap.txt
git log --all --pretty=format:"%an <%ae>" | sort -u
sampleUser <newname@users.noreply.github.com>

最後に書き換えた履歴をリモートへ反映します。

git remote add origin <リポジトリURL>
git push --force --tags origin 'refs/heads/*'

git push --forceを行うとリポジトリの履歴が変更されてしまいます。
他の開発者がいる場合はrebaseまたは再cloneが必要になるため、事前に周知してから実行してください。

参考URL

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?