LoginSignup
8
6

More than 3 years have passed since last update.

githubとgh:eを間違えないためのgitサブコマンドを作った

Last updated at Posted at 2016-06-13

https://github.com/Hi-king/git-confedclone を作りました。

普段会社ではgithub enterpriseを使っているのですが、OSSの仕事でgithubの方にコミットする事もしばしばあります。さらに業務以外のgithubレポジトリにコミットする事もしばしばあるので、ここで問題となるのが、プライベートなプロジェクトに会社のメールアドレスでコミットしてしまう事件です。

configを明示的に書かないとリモートにpushできない設定

git config --global user.useConfigOnly true しておくと、環境変数からemail取ってきて勝手に使っちゃう事がなくなるので、各レポジトリ毎に明示的に git config user.email xxxx@yyyy する必要が出るので、事故らなくなります。( see http://qiita.com/uasi/items/a340bb487ec07caac799 )。

そうすると今度は問題になるのが、レポジトリクローンする毎に設定をしなきゃいけない面倒くささです。怠惰はエンジニアの美徳なので、無駄な作業は一つでも減らしましょう、ということで作ったのが今回のgitサブコマンドです。

使い方

  1. git@github.com:Hi-king/git-confedclone.git
  2. setup.py install
  3. ~/.gitconfedclone をyamlで書く(後述)
  4. git confedclone https://github.com/xxxx/yyyy でcloneとconfig設定が一括でできるようになります。

yaml例

会社とパブリックでメールアドレスを変えたい場合、以下のような設定yamlを書くことでclone元のホストに応じたconfigが自動で書かれます。".*"としている部分はorganizationにマッチさせる正規表現で、ここではホスト名だけで分岐させるという意味です。

- "github.yourcompany.com":
    - ".*":
        "user.name": "John Smith"
        "user.email": "john_smith@yourcompany.com"
- "github.com":
    - ".*":
        "user.name": "J"
        "user.email": "john_smith@gmail.com"
- "bitbucket.org":
    - ".*":
        "user.name": "J"
        "user.email": "john_smith@gmail.com"

githubの中でもorganization毎にメールアドレスを変えたい場合、以下のような設定yamlを書くことで。organization/user毎に別のconfigがclone時に設定されるようになります。

- "github.com":
    - "YOUR-OSS":
        "user.name": "John Smith"
        "user.email": "john_smith@yourcomapany.com"
    - "Other-OSS":
        "user.name": "J.S."
        "user.email": "john_smith@gmail.com"
    - "John":
        "user.name": "J"
        "user.email": "john_smith@gmail.com"

他のやり方

僕は結構困ってるので、他にも困ってる人がいるはずだし、各自いろんな解決をしてるんじゃないかと思います。もっといい方法もあるのかも。少なくとも、gitのpre-commitフックにスクリプトを書く方法はあるとは思います。

8
6
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
8
6