以下の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
以後は
キーチェーンにアカウント情報が格納され、それが使われる
キーチェーン確認
spotlight検索で以下
keychain Access.app
リポジトリ単位でアカウント情報を設定
リポジトリ単位でアカウント情報を設定する
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
に以下を追加する
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
.
.
.
[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
[user]
name = "仕事ユーザ名"
email = "仕事メールアドレス"
[url "github_work"]
insteadOf = git@github.com
remoteのurlを書き換える
git remote set-url origin git@github_work:ユーザー名/リポジトリ名.git