0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GitHubのアカウント別にコミットアカウントを切り替えてpushする設定

Last updated at Posted at 2024-09-16

特定の用途に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.namessh -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.
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?