Git insteadOf Wrong SSH Key: Find the URL Rewrite
If git push uses the wrong SSH key or account, check for a url.<base>.insteadOf rule in your git config. It silently rewrites the remote URL before git connects, so the host alias or key you set up never gets used. Find it with git config --show-origin --get-regexp '^url\.' and remove or scope it.
Short version: git remote -v shows the URL after rewriting, so the repo can look fine while your global config sends it somewhere else.
What does insteadOf actually do?
insteadOf is a git config rule that swaps a URL prefix for another one every time git talks to a remote. The classic example forces SSH instead of HTTPS:
git config --global url."git@github.com:".insteadOf "https://github.com/"
After that, a clone of https://github.com/acme-inc/billing.git is fetched as git@github.com:acme-inc/billing.git. It is handy with one account. With two, it is a trap: the rewritten URL uses the plain github.com host, so SSH offers whichever key it tries first, not the one for the client.
How do I spot a rewrite?
Start with the effective URL, which includes rewriting:
git ls-remote --get-url origin
Compare it with what the repo stores:
git config --get remote.origin.url
If the two differ, a rule is rewriting your remote. List every rule and where it comes from:
git config --show-origin --get-regexp '^url\.'
The --show-origin flag prints the file, so you can tell a global rule in ~/.gitconfig from one in an included file or in the repo’s .git/config.
How do I confirm which key SSH offers?
Ask SSH what it will do for the host in the effective URL:
ssh -G github.com | grep -i -E 'identityfile|identitiesonly'
ssh -vT git@github.com 2>&1 | grep -i -E 'offering|accepted|authenticated'
The first command shows the key files SSH will consider. The second shows which key GitHub accepted and, in the final line, the account name. If that name is not the account you wanted, the rewrite and the key are your culprits. Our guide to multiple SSH keys and IdentitiesOnly covers the key side in detail.
How do I make insteadOf work with two accounts?
Give each account its own host alias in ~/.ssh/config:
Host github-acme
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_acme
IdentitiesOnly yes
Then rewrite only the organization that belongs to that account, instead of all of github.com:
git config --global url."git@github-acme:acme-inc/".insteadOf "https://github.com/acme-inc/"
git config --global --add url."git@github-acme:acme-inc/".insteadOf "git@github.com:acme-inc/"
Git picks the longest matching prefix, so the acme-inc/ rule wins over any broader github.com rule. Repos from other owners keep their normal path. If you only want the rewrite for pushes, use pushInsteadOf, which leaves fetches alone.
Can I scope the rule to a folder?
Yes. Put the rule in a file that only loads inside that folder, using conditional includes:
[includeIf "gitdir:~/clients/acme/"]
path = ~/.gitconfig-acme
[url "git@github-acme:"]
insteadOf = git@github.com:
Everything under ~/clients/acme/ now uses the github-acme alias, and nothing else is touched. This is the same mechanism as a different email per folder with includeIf, including its traps: the path must end with a slash, and the match depends on where the repo really lives.
What else can override the URL?
- A rule in the repo’s own
.git/configadds to the global ones, so check both. - Tools that clone for you, like package managers and CI helpers, may add their own rewrites.
- A rule may exist only to fix an old problem, such as a private package over HTTPS. Remove it once you no longer need it.
To delete a rule: git config --global --unset-all url."git@github.com:".insteadOf.
How do I stop doing this by hand?
Aliases, rewrites and includes work, but they are four files to keep in sync, and they only fix the key: your commit email, gh session and signing still need their own setup. Hats binds a folder to a profile and moves all of them together from the menu bar, and hats doctor flags sneaky URL rewrites like the ones above with the fix next to them.
If your push already failed, see git push permission denied with the wrong GitHub account.
FAQ
Does git remote -v show the rewritten URL?
Yes. It prints the URL after insteadOf is applied, so a repo can look correct while its stored URL is different. Use git config --get remote.origin.url to see the stored value.
Why does git push use my personal key for a work repo?
Most often the remote is git@github.com:..., either stored that way or produced by a global insteadOf rule, and SSH offers your default key for that host. Use a host alias with IdentitiesOnly yes for the work account.
What is the difference between insteadOf and pushInsteadOf?
insteadOf rewrites URLs for both fetching and pushing. pushInsteadOf rewrites only the push URL, so you can fetch over HTTPS and push over SSH.
Does insteadOf change the remote saved in .git/config?
No. The stored URL stays as it was. The rewrite happens at run time, which is why it is easy to miss.
Can I have several insteadOf rules?
Yes. Git uses the longest matching prefix, so specific rules such as one organization override broad ones such as a whole host.