LoginSignup
231
202

More than 5 years have passed since last update.

git configをプロジェクトによって使い分ける

Last updated at Posted at 2017-06-08

Git v2.13.0(2017/05/10リリース)でgit configにConditional includes(条件付きインクルード)という機能が実装され、特定のリポジトリに対して一括でgit configを適用できるようになりました。

会社用と個人用でプロファイルを使い分けたり、たまに使う設定をまとめて適用する場合などに役立ちそうです。

設定方法

includeIfセクションを使います。git configコマンドか、~/.gitconfigを直接編集して追記します。

$ git config --global includeIf."<条件>".path "<設定ファイル>"
.gitconfig
+ [includeIf "<条件>"]
+   path = <設定ファイル>
  • <条件>部分にはgitdir:シンタックスで対象ディレクトリを指定できる。
    • gitdir:では標準のglobに加え、**で階層をまたいだマッチも可能。
    • gitdir:~/foo/bar/のように/で終わる場合は、~/foo/bar/**と補完され子階層すべてにマッチする。
  • <設定ファイル>に記述した内容が、該当ポジトリだけに適用される。

より詳しい仕様は公式ドキュメントで確認できます。

例: 親ディレクトリによってメールアドレスを変える

普段はmyname@example.comを使っているが、いくつかのプロジェクトにはuser@work.comというメールアドレスでコミットしたい場合を考えます。対象プロジェクトをworkディレクトリの下に置き、一括で設定を適用します。

ディレクトリ:

~/
├── a/
├── b/
└── work/
    ├── x/
    └── y/

設定:

.gitconfig
[user]
  email = myname@example.com

...

[includeIf "gitdir:~/work/"]
  path = ~/.gitconfig-work
.gitconfig-work
[user]
  email = user@work.com

確認:

# プロジェクトaではグローバル設定が反映される
$ cd ~/a

$ git config user.email
myname@example.com

# work配下のプロジェクトxでは設定が上書きされている
$ cd ~/work/x

$ git config user.email
user@work.com

このようにincludeIfを使えば、特定の設定を上書きすることができます。
条件を追加して複数の設定ファイルを使い分けることもできます。

参考

231
202
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
231
202