LoginSignup
7

More than 3 years have passed since last update.

WindowsでGitHubの複数アカウントをhttpsで使い分ける

Last updated at Posted at 2020-05-21

はじめに

GitHubの複数アカウントを使い分けるには片方のアカウントでSSH接続する方法があります。ですがGitHubでGit LFSのファイルはhttpsのみでSSHにできないのでダウンロードできません。そこでCredential Managerのnamespaceで複数アカウントをhttpsで使い分けます。

必須環境

Git for WindowsのインストールウィザードでEnable Git Credential ManagerをチェックしてCredential Manager for Gitを一緒にインストールします。

image.png

メインアカウントの設定

特に設定は不要です。git cloneなどを行って認証するとコントロールパネルの資格情報マネージャーのWindows資格情報にgit:https://github.comが追加されます。

image.png

サブアカウントでの設定

Credential Managerでnamespaceを設定すると資格情報の先頭のgit:が変わり複数のアカウントを使い分けることができます。

  1. cloneする時はnamespaceが設定されていないので-c credential.namespaceで指定します。指定する名前はgit以外の英数字なら何でも構いません。

    git -c credential.namespace=sub clone https://github.com/[name]/[repository].git
    
  2. ダイアログが出てくるのでサブアカウントのアカウントとパスワードを入力します。

  3. cloneできたらローカルコンフィグにcredential.namespaceを設定します。

    cd [repository]
    git config credential.namespace sub
    
  4. pushして失敗しない事を確認します。

    git push
    

cloneして認証が通った後に資格情報マネージャーを開くとsub:https://github.comが追加されています。

image.png

詳しくはCredential Manager for Windowsのcredential.namespaceを参照してください。

注意点

git configのConditional Includesを使うとgitdir:で指定したフォルダにcredential.namespaceを設定できそうですがCredential ManagerがConditional Includesに対応していないので利用できません。issuesに要望を上げたので興味ある方はリアクションやコメントをお願いします。

参考リンク

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
What you can do with signing up
7