LoginSignup
33
33

More than 3 years have passed since last update.

複数のgithubアカウントをノンストレスで運用する方法(脱ssh鍵設定)(git credential helperの活用)

Last updated at Posted at 2020-04-13

はじめに

githubは公式でも言っているように、基本的に1人1アカウントで運用することが推奨されています。
しかし、様々な事情で複数のGithub Accountを利用している人も多いのではないでしょうか。
そんな時に困るのが、git commandを使用する時の認証だと思います。

実際にググってみると、以下のような回避方法がありました。

  1. ssh keyを登録して、ssh configを設定して切り分ける方法(一番王道)
  2. 1つのリポジトリだけであれば、 https://username:password@github.com/xxx/xxx でcloneする方法(リポジトリ一つならこれが圧倒的に楽)
  3. .netrc を切り替えて使う方法 (sshの設定しなくていいけど、手動切り替え)

しかし、sshの鍵登録するのもいやだし、すべてのremote設定を git:// にするのも面倒。 go mod もノンストレスで動いて欲しいし、httpsでやりたいけど、毎回手動で切り替えもしたくないとずっと悩んでいました。

そこで少し調べてみると、 git-credential-helper を使えば、うまいこといくんじゃないかと思い、なんとかうまくいきそうだったので、共有します。

git-credential-substitute

image.png

  • 今回 git-credential を利用するにあたって、 git-credential-substitute というものを自作しました。
  • git credential helperを使って、organization単位でのgit認証情報を切り替えます。
  • substitute は linuxの su (substitute user) を意識して名付けました

機能

  • 各githubユーザのクレデンシャルを保存し、organization情報をもとに、適切なクレデンシャルを使用する
  • どのorganizationにも該当しない場合は、デフォルトのクレデンシャルを使用します。
  • gitディレクトリにある場合は、originの組織情報を元にクレデンシャルを判断して使用します。

注意点

  • この git-helper-substituteosxkeychain と一緒には使えません。この osxkeychain の設定をすべて無効にする必要があります。
  • こちらのツールは、push cloneなどの remote repository (githubなど) の認証を切り替える方法です。コミットユーザの切り替えはできませんので、そちらは @ryuta69 さんが書いてくださった コメント を参考にしてください。

設定方法

  • git-credential-substituteのインストール
macOS
brew tap sadayuki-matsuno/git-credential-substitute
brew install git-credential-substitute
golang
go get -u github.com/sadayuki-matsuno/git-credential-substitute

goの環境がある人は上記のコマンドでok。ない人は release からダウンロードが必要

  • $HOME/.git-secret.json の作成
$HOME/.git-secret.json
{
  "another-organization": {
    "username": "another-github-username",
    "password": "xxxxxxxxxxxxxxxxxxxxxxx"
  },
  "default": {
    "username": "sadayuki-matsuno",
    "password": "xxxxxxxxxxxxxxxx"
  }
}

passwordはpersonal token推奨
オーガニゼーションと認証情報のjsonを作成、defaultはデフォルトで利用される認証情報。

  • git-credential-substitute が実行できることを確認
git-credential-substitute

It's working fine.

これが実行できない場合は、PATHの問題の可能性があるので、実行できるように設定する

  • git-credential-helper の確認
git config --show-origin --get credential.helper
  • git-credential-helper の設定変更
git config --global credential.helper substitute

  • git-credential-healper の値が変わっていることを確認する
git config --show-origin --get credential.helper

file:$HOME/.gitconfig    substitute
git config --system --show-origin --get credential.helper

globalとsystemにgitconfigの設定があるので、どちらもチェックして、 osxkeychain がないことを確認する。ある場合は、設定を削除する

  • sysytemに設定されている git-credential-helper を削除 (osxkeychainが設定されている時に実行)
git config --system --unset credential.helper

使い方

  • git clone
    • 現在のディレクトリを使ってクレデンシャル情報を判断するので、git cloneをする際にはorganization名のディレクトリを最初に作成し、そのディレクトリの中で git clone します。
mkdir `${organization_name}`
cd `${organization_name}`
git clone https://github.com/${organization_name}/xxxx
  • その他のgit command
    • git ディレクトリ内でgit commandを利用する場合は、自動的にoriginのorganizationを使って認証情報を決定します。

最後に

認証情報を暗号化するとか、remote origin以外にpushするのにどうするかとかまだまだ課題はありますが、
一旦個人的にはとても満足しています。是非使ってみていただいて、意見や、PRいただけると嬉しいです。

windowsは試して無いですが、mac, linuxは動くはずです。

image.png
omeroid株式会社ではエンジニアを募集しています!
是非興味のある方はお声がけください!

:office: omeroid HomePage
:couple_mm: Wantedly
:thumbsup: Facebook
:hatched_chick: Twitter
33
33
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
33
33