I'm having a problem using two Github SSH keys on my macos where it gets stuck on the first account used after a reboot.
Background:
My mac is set up with two Github accounts - one for a personal
repository and one for a business
repo. The business repo is private. The personal repo is public, but only my personal account has access to push.
System setup:
My system and global configs are empty
~/.ssh/config:
# business
Host business
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
UseKeychain yes
AddKeysToAgent yes
#personal
Host personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
UseKeychain yes
AddKeysToAgent yes
personalreporoot/.git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[user]
name = personalgithubaccount
email = mypersonalemail@pm.me
[remote "origin"]
url = git@personal:mygithubuser/personalrepo
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
remote = origin
merge = refs/heads/main
businessreporoot/.git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[user]
name = businessgithubaccount
email = businessemail@businessdomain.com
[remote "origin"]
url = git@business:businessname/businessrepo
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
remote = origin
merge = refs/heads/main
It all worked as intended until I restarted my computer. I noticed the problem when I was trying to push to my personal repository. It errored saying business account didn't have permission to push to the personal repo.
I spent a number of hours trying through Stack Overflow and internet articles, most of which recommended deleting my macos Keychain git
entries and removing osxkeychain from the config files, but no no avail. Finally, resorting to troubleshooting 101, I rebooted and viola, I could push to the personal account and it used my personal credential now instead of my business credential.
BUT -- then I went to git pull
on the business repo and it said it was unable to find the repository. Figuring I had the reciprocal problem and it was now stuck on my personal account, I rebooted, pulled the business repo and it worked, but now I couldn't push to my personal repository because git was trying to use my business repo credential again.
I found a workaround in This SO article that linked to these instructions. It seems that running $ ssh-add -D
after a reboot will make Git respect each repo's SSH key. But a subsequent macos restart brings the problem back.
So...the questions are, why does Git get pinned to the first SSH credential used after a reboot, why does clearing SSH identities with $ ssh-add -D
fix the problem, and how do I improve my setup so I don't have to do the workaround after restarts?