Skip to content

Instantly share code, notes, and snippets.

@kitsuyui
Last active November 26, 2016 14:17
Show Gist options
  • Save kitsuyui/ae1e8a61ae5b55b84f8b166dfefe840f to your computer and use it in GitHub Desktop.
Save kitsuyui/ae1e8a61ae5b55b84f8b166dfefe840f to your computer and use it in GitHub Desktop.
2 つの異なる GitHub アカウントの SSH を使い分けたい場合のやり方 ref: http://qiita.com/kitsuyui/items/7228f2c1570d8ee108e4
Host github.com
IdentityFile /dev/null
Host foo.github.com
User git
HostName github.com
IdentityFile ~/.ssh/foo.id_rsa
Host bar.github.com
User git
HostName github.com
IdentityFile ~/.ssh/bar.id_rsa
$ git config --global alias.config-foo '!~/.bin/git-config-foo'
$ git config-foo
#!/usr/bin/env bash
set -eu
USERNAME='foo'
EMAIL='[email protected]'
SSH_ALIAS='ssh://foo.github.com/'
GPG_KEY='XXXXX'
upsert_config(){
local key="$1"
local val="$2"
if git config --get "$key" >/dev/null 2>&1 ; then
git config --replace "$key" "$val"
else
git config --add "$key" "$val"
fi
}
upsert_config user.name "$USERNAME"
upsert_config user.email "$EMAIL"
upsert_config url."$SSH_ALIAS".insteadOf '[email protected]:'
# ↓ GPG の設定が必要な場合はアンコメントアウト
# upsert_config user.signingkey "$GPG_KEY"
# upsert_config gpg.program 'gpg1'
# upsert_config commit.gpgsign true
Host github.com
IdentityFile /dev/null
Host foo.github.com
User foo
HostName github.com
IdentityFile ~/.ssh/foo.id_rsa
Host bar.github.com
User bar
HostName github.com
IdentityFile ~/.ssh/bar.id_rsa
$ git config --add url."ssh://bar.github.com/".insteadOf '[email protected]:'
$ git config --add url."ssh://foo.github.com/".insteadOf '[email protected]:'
$ git config --add url."ssh://bar.github.com/".insteadOf '[email protected]:'
$ git config --add url."ssh://foo.github.com/".insteadOf '[email protected]:'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment