Switch between user identities in one Git on one computer [duplicate]

You have your global .gitconfig where you already configured your SSH Keys/User Information. The global .gitconfig is overridden by a local gitconfig - the file "config" in your .git folder (if it does not exist you might have to create it).

For example you can copy the .gitconfig file into the .git folder (and rename it to "config") and just change the lines you want to change (probably github.user and github.token) or you create a new file with just the two lines in it.

If you prefer the command line "git config" you can avoid all the file moving stuff by omitting the "--global" option.


You need to determine if you actually have two ssh keypairs, or just two emails you want to use. An ssh keypair is linked to accounts as described here.

The ssh keypair (specifically the private key), basically gives your git client permission to connect to github, and thus permission to push. This is separate from the user identity, which is just the email in your commit messages.

If you have two ssh keypairs, each linked to one account, follow these instructions to create a ~/.ssh/config file. The key part is to use a different ssh psuedo-host for each account:

# Default GitHub user (joe)
Host github.com
  HostName github.com
  User git
  IdentityFile /Users/joe/.ssh/id_rsa

# Client user (client)
Host github-client
  HostName github.com
  User git
  IdentityFile /Users/joe/.ssh/id_rsa_client

You then use two corresponding remotes:

git clone [email protected]:joe/my_repo.git

and

git clone git@github-client:client/his_repo.git

If you just want to use two emails, you can just give each clone a separate .git/config with the desired [user] settings.