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:

I ended up using different protocols since that will save me effort in configuring one of the accounts.


Since I'm in Windows, I can use Git Bash.
  1. Launch Git Bash
  2. Run: ssh-keygen -t ed25519 -C "your_email@example.com"
  3. If prompted to enter passphrase, make sure you note it somewhere (I saved mine in password manager).
  4. 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
  5. Also note the location of the generated key. In my case, it is ~/.ssh/id_ed25519 or %USERPROFILE%/.ssh/id_ed25519

  1. Launch PowerShell in elevated admin mode.
  2. Make sure the ssh-agent is running:
    • Get-Service -Name ssh-agent | Set-Service -StartupType Manual
    • Start-Service ssh-agent
  3. Launch a separate PowerShell terminal without admin mode.
  4. Run the following command: ssh-add c:/Users/{your_user}/.ssh/id_ed25519
After that, we need to add the key to GitHub: https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account#adding-a-new-ssh-key-to-your-account.

  1. In GitHub, under your profile menu on the top right of the page, select Settings.
  2. Then on the left menu, select SSH and GPG keys.
  3. 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.
  4. Go to the key location. In my case: %USERPROFILE%/.ssh
  5. 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
  6. Paste the content of the key to GitHub and click Add SSH key.
Then I added ssh config as follow:
  1. In the key location, create file with name config. In my case, the file path will be: %USERPROFILE%/.ssh/config
  2. For the content, it will be:
    Host github.com
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_ed25519
  3. 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

Thus, that's the end of my multi accounts journey.

Comments

Popular posts from this blog

AWS EC2 Can't Reach EC2 Metadata Service After Subnet Change

A2 Hosting with .NET Core 2.1

Xcode CodeSign Incorrectly States Password is Incorrect