I like to maintain multiple git accounts, one for Github and other for gitlab. I use gitlab to host my much private data in it. And only try to host that I feel safe to be online on with Github.
I will walk you through how to maintain multiple ssh key that could be used for different git accounts.
This is more specific on using ssh for git authentication.
Generation of SSH keys.
For windows install git-bash, to get working with git on windows. Once this is done. We generate keys using ssh-keygen
ssh-keygen -t ed25519 -C "personal_email@gitlab.com" -f ~/.ssh/id_ed25519_gitlab
ssh-keygen -t ed25519 -C "personal_email@github.com" -f ~/.ssh/id_ed25519_github
This generates public and private keys. This keys are generally present in home directory (even in windows) under .ssh directory
Adding keys to ssh-agent
eval "$(ssh-agent -s)"
Add the keys to
ssh-add ~/.ssh/id_ed25519_gitlab
ssh-add ~/.ssh/id_ed25519_github
Why I need to add these? For 1, you do not want to enter your phase key again, every time you start a new session. It would be wise to add these to rc (Run Command) files, like .bashrc
and .zshrc
.
Update .ssh/config file
This is the important step. Now we need to tell which keys to use when we connect to the remote host.
This is a property file, be careful with the spacings. Use # for adding comments
# Github
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github
# Gitlab
Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_ed25519_gitlab
There is other way of seeing as well, adding context to the Host itself. Let say you are working on a machine provided by an organization and want to just check your own repository now and then, but don't want to use keys and user-names of the organization. We can do the following
# Work
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github_work
# Personal
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github_personal
Now when we access the repository we provide context.
Update public keys to services
Now we have created our keys, we need to publish the public key to the service, so it used used when authenticating.
Copy the public key of github, you will see .pub
at the end of the keys.
cat ~/.ssh/id_ed25519_github
Now go to Github -> Settings -> SSH and GPG keys
Now copy the public key of gitlab
cat ~/.ssh/id_ed25519_gitlab
Now go to Gitlab -> Preferences -> SSH Keys
Verify connection
Once the keys are added, it is good practice to check the connections.
ssh -T git@github.com
ssh -T git@gitlab.com
Make sure to use right Host, if you have setup with "-work" and "-personal", use them
ssh -T git@github.com-personal
This should display the username, the keys were added to.
Using correct host names
Now when I use gitlab I will do something like
git clone git@gitlab.com:my-name/repo.git
But when we have multiple github accounts, we could use the context I mentioned before to identify which ssh-key to use, like
git clone git@github.com-work:yourcompany/repo.git
Updating git config
With the above steps, we are complete with setting up ssh. Which is used for authentication.
Now we would want to update gitconfig. Run
git config user.name
git config user.email
commands. This will give the global name and email used. We would need to update our local name and local email, as they would want to reflect in the git logs.
Make sure you are in the repository where you want the name to be updated.
git config --local user.name "<USER_NAME>"
git config --local user.email "<private_email@example.com>"
Leave any questions or suggestions you want to answer or improve on.
Top comments (0)