Home > Blockchain >  Git pushing with wrong username when having multiple git accounts locally
Git pushing with wrong username when having multiple git accounts locally

Time:10-31

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:

3072 SHA256:/Vq3tN5FxtE64LALAe25GQr MpIPbGg [email protected] (RSA)
3072 SHA256:9NheazRnzMzicLALA6z70kQeO6tQcNZcePJw0RRk [email protected] (RSA)
3072 SHA256:r7uaTSfE9ZXn7LALAGHIn4syyaKPPyXsKdK8Sjk [email protected] (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 = [email protected]
[remote "origin"]
    url = [email protected]: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        [email protected]
local   file:.git/config        [email protected]: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  [email protected]:user/repo.git (fetch)
origin  [email protected]: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!

CodePudding user response:

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.)

CodePudding user response:

Try changing your origin to :

[email protected]:user/repo.git

  • Related