2

I currently have two git users for two different Github account repositories on the same machine.

Somehow, it sounds to me that the first user I have set on my machine is always the one taken into account when I attempt to push to origin. When I try to push using the second user, I get a message saying that the first user has no rights on the second repository.

I would like as a first step to override the 'system' config in the second repo, but it would then be nice to know how is git generally configured on a Mac machine, and how can I make multiple users linked to multiple Github accounts coexist on the same machine?

N. Labrahmi
  • 657
  • 4
  • 23
  • Checkout this link https://stackoverflow.com/questions/3860112/multiple-github-accounts-on-the-same-computer – diypcjourney Feb 19 '18 at 23:19
  • @diypcjourney I've already set up ssh keys for each account, but somehow only the first configured user seems to be taken into account. It would be maybe be more helpful to know about how git is configured to figure out the reason behind this behavior. – N. Labrahmi Feb 19 '18 at 23:21
  • *It would be more helpful to know about how git is configured* Yes, exactly. So please show us your configuration. Especially your remotes. – phd Feb 20 '18 at 04:21

2 Answers2

3

SSH will by default use keys in a fixed order either based on what they're named on the system or what's in the SSH agent. It sounds like you're always using the same key when you push to GitHub, resulting in you always using the same user.

The easiest way to handle this is to create an SSH alias for GitHub that uses the appropriate identity. For example, in your ~/.ssh/config file, you could have

Host github-user1
    HostName github.com
    User git
    IdentityFile ~/.ssh/user1
    IdentitiesOnly yes
Host github-user2
    HostName github.com
    User git
    IdentityFile ~/.ssh/user2
    IdentitiesOnly yes

Then, configure your Git remotes use either github-user1 or github-user2 as a hostname instead of github.com. For example, git remote set-url origin github-user1:git/git.git.

bk2204
  • 64,793
  • 6
  • 84
  • 100
  • Thanks for the explanation, I'll try configuring users this way when I can and I'll get back to you. Meanwhile, could you elaborate on what you mean by the 'fixed order [...] or what's in the SSH agent'? – N. Labrahmi Feb 20 '18 at 07:46
  • Sure. Whether you're using an SSH agent (`ssh-add -l` will show you its contents) or not, the SSH keys will always be loaded in the same order, so ssh will always try the same key first. GitHub knows which user you are by SSH key, so trying the same key will always result in authenticating as the same user. – bk2204 Feb 20 '18 at 11:46
  • 1
    The Github repository is not recognized when I use the syntax you provided for the remote: 'github-user1:git/git.git'. Probably a dumb question but could you clarify which are the members of that expression? (which is the real Github username, the repository name etc.) – N. Labrahmi Feb 20 '18 at 22:38
  • 1
    Figured it out eventually, that would be: git@custom-hostname:github-username/repo-name.git, and now I am able to use that specific user for that specific repository. Thanks again for your help. – N. Labrahmi Feb 20 '18 at 22:46
0

To set user on the local repository, run these commands at the root of your repository.

git config user.name "Your Name Here"
git config user.email your@email.com

To set the user on the global level, run these commands

git config --global user.name "Your Name Here"
git config --global user.email your@email.com
diypcjourney
  • 209
  • 2
  • 6
  • I have set the user both on the repository and the global levels, but once I try to push, I am prompted with the same message saying that the other user is not authorized on this repository. – N. Labrahmi Feb 19 '18 at 23:30
  • I think you need to check which user authored the repository and if it's not the account you want to push with, just share and give rights to repository to the user account you want to push with. – diypcjourney Feb 19 '18 at 23:33
  • 3
    This configuration doesn't effect the user used for ssh authentication. It is just for labels on your commits. – merlin2011 Feb 19 '18 at 23:45