9
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Gitのコミッタを修正したい。

Posted at

「しまった!職場なのに適当に設定してたら私用メールアドレスでGitのコミットしてしまった!!!」

みたいな経験ありませんか?自分は両方のパターンを経験しましたorz

そのような不幸な事故を起こした時に変更する方法、およびそれを再発しないための方法を備忘録的に書いておきます。

コミッターをリポジトリごとに設定する

普段は

global
$ git config --global user.name deflis
$ git config --global user.email deflis@home.example.com

のように設定すると思いますが、ここの--globalを消すことでリポジトリごとに設定することが出来ます。

local
$ git config user.name deflis
$ git config user.email deflis@coop.example.com

手間は増えますが、会社メアドを晒すような事故が減らせるので一手間加えてでもやりましょう。

過去のコミットのコミッターを修正する

一つ前のコミットを修正する

bad
$ git commit --amend

上のことをやってからamendすれば問題ないだろう、と普通は考えてしまうのですが、ダメです。
Authorに前のアカウントが残ってしまいます。

なので、正しいやり方は

bad
$ git commit --amend --author="my_name <my_name@example.com>"

というように、authorを別途指定するのが正しいやり方です。

複数のコミットを修正する

rebase -i を使う方法

rebase
$ git rebase -i

で書き換えたいコミットをすべてedit指定します。そして、上の手順でamendします。
正直めんどくさいですが、確実です。

filter-branch を使う方法

後で書きます。

9
10
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
9
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?