Git HTTPS Credential Helper がややこしいので SSH に切り替える。
GitHub リポジトリを指す remote origin の変更が必要となりますので、ざっくり概要を見ておきます。
# HTTPS 利用していたとする
git remote -v
> origin https://github.com/OWNER/REPOSITORY.git (fetch)
> origin https://github.com/OWNER/REPOSITORY.git (push)
# SSH 形式の URL に変更
git remote set-url origin [email protected]:OWNER/REPOSITORY.git
# 確認
git remote -v
# Verify new remote URL
> origin [email protected]:OWNER/REPOSITORY.git (fetch)
> origin [email protected]:OWNER/REPOSITORY.git (push)
👉 リモートリポジトリを管理する - GitHub Docs
🧑🏻💻 手順
1. 鍵をPC側で作成。
❯ ssh-keygen -t ed25519 -C [email protected] -f ~/.ssh/id_ed25519_main
👉 新しい SSH キーを生成して ssh-agent に追加する - GitHub Docs
2. GitHub にログインして貼る。Authentication keys
❯ pbcopy < ~/.ssh/id_ed25519_main.pub
3. ~/.ssh/config を設定する。
❯ vi ~/.ssh/config
Host github-main
HostName github.com
User git
Port 22
IdentityFile ~/.ssh/id_ed25519_main
TCPKeepAlive yes
IdentitiesOnly yes # need for multiple accounts
Host github-sub
HostName github.com
User git
Port 22
IdentityFile ~/.ssh/id_ed25519_sub
TCPKeepAlive yes
IdentitiesOnly yes # need for multiple accounts
4. PC側プロジェクト内で remote origin を書き換える。
git@github-main:your-main/Sample.git
❯ git remote -v
origin https://github.com/your-main/Sample.git (fetch)
origin https://github.com/your-main/Sample.git (push)
❯ git remote set-url origin git@github-main:your-main/Sample.git
❯ git remote -v
origin git@github-main:your-main/Sample.git (fetch)
origin git@github-main:your-main/Sample.git (push)
❯ cat .git/config
[remote "origin"]
url = git@github-main:your-main/Sample.git
fetch = +refs/heads/*:refs/remotes/origin/*
5. 接続を確認する。
❯ ssh -T git@github-main
Hi your-main! You've successfully authenticated, but GitHub does not provide shell access.
👉 [備忘] 複数Githubアカウントでssh接続設定(config)を使い分ける手順 #GitHub - Qiita
🧑🏻💻 まとめ
URL 記述部分は、ssh:// がついてるほうが意味が分かりやすい気がします。
その場合は ~/.ssh/config の port 22 の記述は不要のようですが、
現在 GitHub ではスキーマ部分消えています。
などとダラダラ書きましたが、以下読んでみるとよく分かります。
👉 Multiple GitHub Accounts & SSH Config - Stack Overflow
👉 Using multiple github accounts with ssh keys
👉 【Git】Obsidian を GitHub と連携する