Sometimes it happen when we have to use multiple GitHub accounts on a single machine, but it quite tricky to handle in a proper way. Let me describe full steps including some issues I’ve faced and its fixes.
Step 1 – Setup SSH Keys
# lets assume the first user account is: user1@admin.ge
# and the second one: user2@admin.ge
# use git bash
# change directory to ~/.ssh/ which located on: %USERPROFILE%\.ssh
$ cd ~/.ssh/
$ ssh-keygen -t rsa -C user1@admin.ge
# skip passphrases and the output will look like:
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Admin/.ssh/id_rsa): id_rsa_user1
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_rsa_user1
Your public key has been saved in id_rsa_user1.pub
The key fingerprint is:
SHA256:9aksjdfhkjh/YdkfjsljkfyAjduwiuywerhjk user1@admin.ge
The key's randomart image is:
+---[RSA 3072]----+
|OBO. + |
|+O+=B+o . |
..
# same for the second user
$ ssh-keygen -t rsa -C user2@admin.ge
..
Enter file in which to save the key (/c/Users/Admin/.ssh/id_rsa): id_rsa_user2
..
In result four files will be generated in ~/.ssh
:
id_rsa_user1
id_rsa_user1.pub
id_rsa_user2
id_rsa_user2.pub
.
Step 2 – Prepare configuration file
In ~/.ssh/
( %USERPROFILE%\.ssh
) create a config
and enter the keys we’ve just generated:
# github for user1@admin.ge
Host user1
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_user1
# github for user2@admin.ge
Host user2
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_user2
.
Step 3 – Add newly generated keys to GitHub.com
Go to GitHub.com ➤ Settings ➤ SSH and GPG keys ➤ SSH Keys ➤ New SSH Key:
Enter some title and in the Key
field enter the public key content from ~/.ssh/id_rsa_user1.pub
and ~/.ssh/id_rsa_user2.pub
to the related GitHub accounts respectively.
.
Step 4 – Register Identities
# check already existed identities
$ ssh-add -l
# if there is a case & you'd like to delete already existed ones, use the next command:
$ ssh-add -D
# register new ones
$ ssh-add.exe id_rsa_user1
Identity added: id_rsa_user1 (user1@admin.ge)
$ ssh-add.exe id_rsa_user2
Identity added: id_rsa_user2 (user2@admin.ge)
But, if you face the next type error, like:
Could not open a connection to your authentication agent.
Then you’ve to initialize the ssh-agent:
$ exec ssh-agent bash
.
Step 5 – Verify
To verify configuration we need to test prepared keys by running next commands:
$ ssh -T user1
If you see errors like: ssh: Could not resolve hostname user1 ..
or smth else, please check the previous steps.
In case of success you should see the message smth like this:
# Hi <USER1_USERNAME>! You've successfully authenticated, but GitHub does not provide shell access.
The same should appear in case of running: ssh -T user2
command, but be careful, if the
WARNING: If in the second time you’ll see the same username for the user1
, it mans that there are some errors, might be the key was not registered to the proper user, so git by default will use the user1
account instead. So be sure that the testing key output will be with the proper username, like:
# Hi <USER2_USERNAME>! You've successfully authenticated, but GitHub does not provide shell access.
.
Step 6 – Test PUSH Requests
$ git remote add origin git@user1:some-github-access-granted-account/project.git
$ git push origin main
$ git remote add origin git@user2:some-github-access-granted-account/project.git
$ git push origin main
WARNING: Be careful, when submitting with second user, despite that correct user account (in our case user2
) is used, author on GitHub.com still could be the user1
‘s name, in case if both the users have access to the shared project. At least, I faced the problem at the moment there is a very simple solution for it, namely on user2
‘s copy of repo set local git configuration using next command:
$ git config --local user.email user2@admin.ge
.
NOTES
test user in verbose mode:
$ ssh -T user1
If getting error like: Bad owner or permissions on ~/.ssh/config
change permissions:
chmod 600 ~/.ssh/config
Config path on CentOS for regular users:
/etc/ssh/ssh_config
.
@source:
https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
https://github.com/ritazh/devopsfun/issues/17#issuecomment-189014524
https://linux.die.net/man/1/ssh-add
https://docs.github.com/en/authentication/connecting-to-github-with-ssh/testing-your-ssh-connection
https://docs.github.com/en/pull-requests/committing-changes-to-your-project/troubleshooting-commits/why-are-my-commits-linked-to-the-wrong-user
One thought on “How to manage multiple GitHub accounts using git on Windows and fix git push with wrong user name”