Y-Ken Studio

新しもの好きのデータエンジニアが四方山話をお届けします。

Gitコミットを任意の名前とメールアドレスで行うコマンド

git commitを行う際、任意の名前・メールアドレスで行いたい時、ありませんか?
だからといって、以下のように一時的に変更して元に戻すのも面倒ですよね。

そんな時にオススメしたい方法、あるんです。

グローバル設定を変更する
ホームディレクトリ/.gitconfigで定義する情報を変更するコマンド

$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com

ローカルリポジトリ設定を変更する
リポジトリ/.git/configで定義する情報を変更するコマンド

$ git config user.name "John Doe"
$ git config user.email johndoe@example.com

ユースケース

  • sudo git commit -aコマンドを利用している
    いわゆる、コミットログがroot名義(Author: root <root@ホスト名>)となる問題に悩まされている

  • .gitconfig.git/configの変更無しに任意の名義でコミットしたい
    いちいち変えたり、元通りにするのは面倒ですよね・・・

などなど、シチュエーションは色々あると思います。

コマンド

はい、ここ重要ですよ。

--author引数を利用して名前とメールアドレスを指定する事で、今回の課題も解決です。

git commit --author='Author Name <author@email.address.com>' -a

補足説明
git logで通常表示されるAuthorはこのコマンドで指定した通りになりますが、
以下のようにCommiterへは作用しません。問題となるケースは希だと思いますけども。

$ sudo git log
commit fadb18b1771895b5fd8f11db63f7dd951decb67d
Author: Author Name <author@email.address.com>
Date:   Sat Apr 20 00:23:08 2013 +0900

    test commit

$ sudo git log --pretty=full
commit fadb18b1771895b5fd8f11db63f7dd951decb67d
Author: Author Name <author@email.address.com>
Commit: root <root@localhost>

    test commit

参考ページ

http://serverfault.com/questions/256754/correct-user-names-when-tracking-etc-in-git-repository-and-committing-as-root