特定の用途にGitHubアカウントを分けたものの
コミッターが元のGitHubアカウントのcommitterのままpushされてしまい困ったので、
GitHubアカウント別にそれぞれのアカウントでcommitしてpushしたい。
ディレクトリ毎に自動でGithubアカウントを切り替え設定してcommit&pushできる方法。
意外に面倒だったのでやり方メモ
sshの設定
2つのgithubアカウントのため、sshを別々に用意します
ssh秘密鍵公開鍵キーペアを払い出ししている前提とします。
$ cd ~/.ssh
$ ssh-keygen
./ssh/config
を編集します。
ServerAliveInterval 30000
TCPKeepAlive yes
Host github
HostName github.com
IdentityFile ~/.ssh/id_rsa_github
User git
IdentitiesOnly yes
Host sub.github
HostName github.com
IdentityFile ~/.ssh/id_rsa_sub_github
User git
IdentitiesOnly yes
Hostのエイリアスを変えてサブアカウントのものを作成します。
(HostNameは同じ)
IdentityFileはssh秘密鍵を指定します。
切り替え用の.gitconfigを用意
サブアカウント用に~/.gitconfig_sub
を作成します。
urlにはサブGithubアカウントを指定し、
push時のssh認証をサブアカウントのもので行う。
[user]
email = sub@gmail.com
name = sub
[url "ssh://sub.github/"]
insteadOf = git@github.com:
特定のディレクトリのみ、
(今回は~/Desktop/sub/
で切り替える想定)
サブGithubアカウントの~/.gitconfig
の設定を適用します
(この例だと、~/Desktop/sub
にサブGithubアカウントのリポジトリをクローンしている想定)
[user]
email = main@gmail.com
name = main
[includeIf "gitdir:~/Desktop/sub/"]
path = ~/.gitconfig_sub
動作確認
~/Desktop/sub/
にて
github config user.name
とssh -T (Host)
で
GitHubアカウントが切り替わっているか確認
$ git config user.name
main
$ ssh -T github
Hi (メインGitHubのアカウント名)! You've successfully authenticated, but GitHub does not provide she
ll access.
$ cd ~/Desktop/sub
$ git config user.name
sub
$ ssh -T sub.github
Hi (サブGitHubのアカウント名)! You've successfully authenticated, but GitHub does not provide shell
access.