Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Git pushing with wrong username when having multiple git accounts locally

I use three different GitHub accounts.

Git version: 2.34.1
OS: Ubuntu 22.04

I have correctly set my ssh keys (created, added to keyring, added to github).

Doing ssh-add -l returns:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

3072 SHA256:/Vq3tN5FxtE64LALAe25GQr+MpIPbGg mail@first.com (RSA)
3072 SHA256:9NheazRnzMzicLALA6z70kQeO6tQcNZcePJw0RRk mail@second.com (RSA)
3072 SHA256:r7uaTSfE9ZXn7LALAGHIn4syyaKPPyXsKdK8Sjk mail@third.com (RSA)

In my ~/.ssh/.config file I have:

# GITHUB FIRST
Host github.com-first-user
  HostName github.com
  User git
  IdentityFile ~/.ssh/first-user

# GITHUB SECOND
Host github.com-second-user
  HostName github.com
  User git
  IdentityFile ~/.ssh/second-user
  
# GITHUB THIRD
Host github.com-third-user
  HostName github.com
  User git
  IdentityFile ~/.ssh/third-user

In my global git config I did not add any user/email config.

Each repository contains the correct user and email.

This is an example local git config for one repository:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    sshCommand = ssh -i ~/.ssh/second-user
[user]
    name = Second
    email = mail@second.com
[remote "origin"]
    url = git@github.com-second-user:user/repo.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
    remote = origin
    merge = refs/heads/main

When I try to push, it says that first-user doesn’t have permission.

ERROR: Permission to user/repo.git denied to first-user.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

I believe to have read somewhere that git uses the ssh key it sees, so maybe it is seeing the first key and taking that.

Doing git config --list --show-origin --show-scope in the repo folder, I get:

global  file:/home/afranz/.gitconfig    core.autocrlf=input
global  file:/home/afranz/.gitconfig    safe.directory=*
local   file:.git/config        core.repositoryformatversion=0
local   file:.git/config        core.filemode=true
local   file:.git/config        core.bare=false
local   file:.git/config        core.logallrefupdates=true
local   file:.git/config        core.sshcommand=ssh -i ~/.ssh/second-user
local   file:.git/config        user.name=Second
local   file:.git/config        user.email=mail@second.com
local   file:.git/config        remote.origin.url=git@github.com-second-user:user/repo.git
local   file:.git/config        remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
local   file:.git/config        branch.main.remote=origin
local   file:.git/config        branch.main.merge=refs/heads/main

Nowhere is there any setup for the first user.

Doing git remote -v returns:

origin  git@github.com-second-user:user/repo.git (fetch)
origin  git@github.com-second-user:user/repo.git (push)

Relevant note:

Doing ssh -T github.com-second-user returns Hi first-user!, You’ve successfully authenticated…

Note: this error happens both using the git cli, and also using git via the phpStorm UI.

How can I force it to use the second key for the second user?

Thanks!

>Solution :

Add:

IdentitiesOnly yes

(either to each Host entry for the three aliases, or globally). Without this setting, ssh first tries all the agent-supplied identities, then tries the file listed. Since the first agent-supplied entity works as the first user, that gets you in to GitHub as the first user: the second user’s key is never attempted, and GitHub believe you are the first user.

With IdentitiesOnly yes, ssh tries only the listed IdentityFile entries, in the order they appear (still getting keys from the agent as needed, so that you need only store the .pub files on the computer in question, if that’s not your primary system).

(Nothing Git does here makes any difference: all of this is entirely up to ssh and GitHub.)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading