Use Multiple Git Accounts on One Computer
I was looking for a way to use two git accounts in a single machine. Apparently, there are multiple ways to do that:
- Use different protocols for different accounts. One account can use HTTP, another account uses SSH.
- Use different SSH keys. One per account.
- Use HTTP and PAT. This might be GitHub specific but per my experience with PAT in Azure DevOps, this is not feasible as PAT has expiration and needs to be renewed. https://docs.github.com/en/account-and-profile/setting-up-and-managing-your-personal-account-on-github/managing-your-personal-account/managing-multiple-accounts
I ended up using different protocols since that will save me effort in configuring one of the accounts.
First, I need to create an ssh key. https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#generating-a-new-ssh-key.
Since I'm in Windows, I can use Git Bash.
- Launch Git Bash
- Run: ssh-keygen -t ed25519 -C "your_email@example.com"
- If prompted to enter passphrase, make sure you note it somewhere (I saved mine in password manager).
- If you need to change the passphrase, follow the steps in this link: https://docs.github.com/en/authentication/connecting-to-github-with-ssh/working-with-ssh-key-passphrases#adding-or-changing-a-passphrase
- Also note the location of the generated key. In my case, it is ~/.ssh/id_ed25519 or %USERPROFILE%/.ssh/id_ed25519
Once the key is generated, we need to add it to ssh_agent. This way, local git will know to use the key. https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#adding-your-ssh-key-to-the-ssh-agent
- Launch PowerShell in elevated admin mode.
- Make sure the ssh-agent is running:
- Get-Service -Name ssh-agent | Set-Service -StartupType Manual
- Start-Service ssh-agent
- Launch a separate PowerShell terminal without admin mode.
- Run the following command: ssh-add c:/Users/{your_user}/.ssh/id_ed25519
- In GitHub, under your profile menu on the top right of the page, select Settings.
- Then on the left menu, select SSH and GPG keys.
- Add a new key and use a descriptive name it under Title box. In my case, I use the name of my machine since the key resides in my machine.
- Go to the key location. In my case: %USERPROFILE%/.ssh
- Copy the content of the {key}.pub (note the .pub extension for public key, don't copy the one without it as it is the private key). In my case, it is id_ed25519.pub
- Paste the content of the key to GitHub and click Add SSH key.
Then I added ssh config as follow:
- In the key location, create file with name config. In my case, the file path will be: %USERPROFILE%/.ssh/config
- For the content, it will be:
Host github.com HostName github.com User git IdentityFile ~/.ssh/id_ed25519
- If there are multiple ssh keys for multiple accounts, it will contain multiple entries where the Host can be different while the HostName can stay the same.
Afterwards, I had to configure global .gitconfig in %USERPROFILE% by adding the includeIf section. This is so that I can use different user name and email for different accounts. The content looks like the following:
[user]
name = {username}
email = {email}
[includeIf "gitdir:~/{other_folder}/"]
path = ~/{other_folder}/.gitconfig
As you noticed, the includeIf will need a separate directory with its own .gitconfig file. It works for me as I have a separate directory for repositories that use a separate git account. The content of the other .gitconfig file will be:
[user]
name = {other_username}
email = {other_email}
By now, it is pretty much done. If I do a git clone on ssh path, it will prompt for my passphrase and it will work.
git clone git@github.com:{account}/{repo}.git
But in my case, I have existing repositories that needs to be updated to use ssh, so for each repo, I had to run:
git remote set-url origin git@github.com:{account}/{repo}.git
Comments
Post a Comment