7
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

GitHubの複数アカウントを使い分け

Last updated at Posted at 2021-01-03

以下の2つのやり方がある

  • https を使った方法
  • SSH を使った方法

私の環境ではhttps を使った方法がしっくりきたのでメモ

https を使った方法

キーチェーンを使うよう credential.helper という config を設定します。

git config --global credential.helper osxkeychain

gitの設定

git config --global user.name "your username"
git config --global user.email "your mailaddress"

確認

git config --global -l

リポジトリを最初にcloneするときだけ「ユーザー名@」をつけてcloneする

username=xxxxxxxxの場合は以下

https://xxxxxxxx@github.com/hogehoge/hoge.git

以後は
キーチェーンにアカウント情報が格納され、それが使われる

スクリーンショット 2020-12-21 17.12.12.png

キーチェーン確認

spotlight検索で以下

keychain Access.app

スクリーンショット 2021-01-14 10.20.39.png

リポジトリ単位でアカウント情報を設定

リポジトリ単位でアカウント情報を設定する

git config --local user.name "your username"
git config --local user.email "your mailaddress"
git config --local -l

SSH を使った方法

新しいSSH Keyの作成

Private用

Key作成

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

SSH Keysの保存先を聞かれているので、id_rsa_github_privateを入力

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/you/.ssh/id_rsa): id_rsa_github_private

パスフレーズの入力を求められるので入力。
ここでは例なので何も入力せずにEnterキーを押した

Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 

Your identification has been saved in id_rsa_github_private.
Your public key has been saved in id_rsa_github_private.pub.
The key fingerprint is:
SHA256:xxxxxxxxxxxxx/xxxxxxxxxxxxx your_email@example.com
The key's randomart image is:
+---[RSA 4096]----+
|  . .o o...      |
|  Eo. . o        |
+----[SHA256]-----+

クリップボードにコピー

上記処理で作成したKeyをクリップボードにコピー

pbcopy < ~/.ssh/id_rsa_github_private.pub

GithubにSSH登録

GithubのSSH 設定画面へ行き、Keyの欄へクリップボードをペースト
https://github.com/settings/ssh/new

Work用

Key作成

ssh-keygen -t rsa -b 4096 -C "your_email@work.co.jp"

SSH Keysの保存先を聞かれているので、id_rsa_github_workを入力

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/you/.ssh/id_rsa): id_rsa_github_work

パスフレーズの入力を求められるので入力。

クリップボードにコピー

上記処理で作成したKeyをクリップボードにコピー

pbcopy < ~/.ssh/id_rsa_github_private.pub

GithubにSSH登録

GithubのSSH 設定画面へ行き、Keyの欄へクリップボードをペースト
https://github.com/settings/ssh/new

~/.ssh/configの設定

~/.ssh/configに以下を追加する

~/.ssh/config
Host github_private
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_rsa_github_private
	IdentitiesOnly yes
	UseKeychain yes
	AddKeysToAgent yes

Host github_work
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_rsa_github_work
	IdentitiesOnly yes
	UseKeychain yes
	AddKeysToAgent yes

接続確認

$ ssh -T github_private
$ ssh -T github_work

運用

グローバル情報を削除する

Gitリポジトリとアカウントの紐付けを間違わないように(確実に意図するアカウントで運用できるように)グローバル情報を削除します。

git config --global --unset user.name
git config --global --unset user.email

消えたか確認してみる

git config user.name
git config user.email

# これで何も表示されなければOK

念のため、~/.gitconfigの中身も確認する

vi ~/.gitconfig

Private用

gitの設定追加

git config --local user.name "個人ユーザ名"
git config --local user.email "個人メールアドレス"
git config --local url."github_private".insteadOf "git@github.com"

※ url."github_private".は、~/.ssh/configに設定したHOST名 + .instea

.git/configの確認

vi .git/config
.git/config
.
.
.
[user]
        name = your name
        email = your address
[url "github_private"]
        insteadOf = git@github.com

Work用

gitの設定追加

既存のリポジトリを開いて以下を行う

git config --local user.name "仕事ユーザ名"
git config --local user.email "仕事メールアドレス"
git config --local url."github_work".insteadOf "git@github.com"

※ url."github_work".は、~/.ssh/configに設定したHOST名 + .instea

.git/configの確認

vi .git/config
.git/config
[user]
        name = "仕事ユーザ名"
        email = "仕事メールアドレス"
[url "github_work"]
        insteadOf = git@github.com

remoteのurlを書き換える

git remote set-url origin git@github_work:ユーザー名/リポジトリ名.git

参考

複数のGitアカウントを手動切替不要で運用する - Qiita

GitHubの複数アカウントを使い分けるならSSHよりhttpsの方がいいんじゃね?という話 - Qiita

7
10
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
7
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?