LoginSignup
215
148

More than 5 years have passed since last update.

GitのCommitユーザを修正する方法

Posted at

githubのcontributionsを見ていたら確かにリポジトリ更新したはずなのに草が生えてなかった。
contributions Activityを見ると、なぜか違うユーザでcommitしたことになっている。
どうやら、メインのPCはMacなのだが、たまにサブの Windows からおこなったcommitが別ユーザで実施したことになっているらしい。そこで git の commit ユーザの確認と、過去のcommitユーザの修正方法についてまとめておく。

CommitterとAuthorの違い

まず過去のcommitのlogを見るにはgit logを使う。

$ git log --pretty=fuller
commit xxxxx085dd658593ca4c42f81ea636134171cec7 (HEAD -> master, origin/master)
Author:     abc <abc@xxx.com>
AuthorDate: Fri Feb 1 00:22:06 2019 +0900
Commit:     abc <abc@xxx.com>
CommitDate: Fri Feb 1 00:22:06 2019 +0900

このとき、
Authorは、オリジナルのCode作成者
Committerは、コミットをした人
になる。
gitは、rebaseやcommit --amendを用いてhistoryを変更できる場合がある。なので元の作成者を分けて管理しているらしい。

Author と Committer 変更方法

commitする際に記録される AuthorとCommitterはローカルリポジトリの .git/config に保存されている。この設定は次で確認できる

git config --list

なお個別の項目は次のように表示する

git config user.name
git config user.email

ここで次のように user.name と user.email を変更する。

git config user.name "hogehoge"
git config user.email hoge@fuga.com

そうすると以後のcommitでAuthorとCommitterが変更される

$ git log --pretty=full
commit xxed4a4c145fb1bc624f34331354795760168ab9 (HEAD -> master, origin/master)
Author: hogehoge <hoge@fuga.com>
Commit: hogehoge <hoge@fuga.com>

過去のCommitのAuthorとCommitterを変更する方法

過去の全てのcommitについて変更したい場合以下を実行する

 git filter-branch -f --env-filter \
  "GIT_AUTHOR_NAME='new'; \
   GIT_AUTHOR_EMAIL='new@example.com'; \
   GIT_COMMITTER_NAME='new'; \
   GIT_COMMITTER_EMAIL='new@example.com';" \
  HEAD

変更が反映されていることを確認

$ git log --pretty=full
commit f2ff63e0090750f06c2a9115960220e0a21b9bb9 (HEAD -> master)
Author: new <new@example.com>
Commit: new <new@example.com>

githubなどのリモートリポジトリへの反映はこの状態で強制的にpushする。

git push -f

補足

特定条件のcommitのみ修正したい場合は、次のように条件を追記する。

git filter-branch --commit-filter ' 
  if [ "$GIT_COMMITTER_EMAIL" = "new@example.com" ];
    then
        GIT_COMMITTER_NAME="new2";
        GIT_AUTHOR_NAME="new2";
        GIT_COMMITTER_EMAIL="new2@example.com";
        GIT_AUTHOR_EMAIL="new2@example.com";
        git commit-tree "$@";
    else
        git commit-tree "$@";
    fi'  HEAD
215
148
3

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
215
148