Skip to content

Instantly share code, notes, and snippets.

@tkaczenko
Created March 25, 2024 15:15
Show Gist options
  • Save tkaczenko/d04719d942aa0320325cdff4c738b2a1 to your computer and use it in GitHub Desktop.
Save tkaczenko/d04719d942aa0320325cdff4c738b2a1 to your computer and use it in GitHub Desktop.
Work With Multiple Git accounts

Work With Multiple Git accounts

Generate SSH key

Github

Full instruction is here.

  1. Run command.
ssh-keygen -t ed25519 -C "your_email@organization_domain"
  1. Specify filename in %USERPROFILE%/.ssh/github.
  2. Add generated public SSH key into Github account. Instruction.

Bitbucket

Full instruction is here

  1. Run command
ssh-keygen -t ed25519 -C "your_email@organization_domain"
  1. Specify filename %USERPROFILE%/.ssh/bitbucket
  2. Add generated public SSH key into Bitbucket account. Instruction

Gitlab

Full instruction is here

  1. Run command
ssh-keygen -t ed25519 -C "your_email@organization_domain"
  1. Specify filename %USERPROFILE%/.ssh/gitlab
  2. Add generated public SSH key into Gitlab account. Instruction

SSH-agent

  1. Kill SSH-agent if exists and run in the background
killall ssh-agent; eval "$(ssh-agent)"
  1. Add private SSH keys to the ssh-agent
ssh-add ~/.ssh/github
ssh-add ~/.ssh/bitbucket
ssh-add ~/.ssh/gitlab
  1. Optional. Copy SSH keys into the Windows directory or any other WSL
cp -b ~/.ssh/github /mnt/c/Users/%USERPROFILE%/.ssh/
cp -b ~/.ssh/bitbucket /mnt/c/Users/%USERPROFILE%/.ssh/
  1. Test your connection
ssh -T git@github.com
ssh -T git@bitbucket.org
ssh -T git@gitlab.com

Configurate ssh-agent for multiple SSH keys

The next steps should be done in each environment where you need access to Git (Windows, WSLs, etc.)

  1. Create config using nano
nano ~/.ssh/config
  1. Put the next config into %USERPROFILE%/.ssh/config
Host github.com
        HostName github.com
        User git
        IdentityFile ~/.ssh/github

Host github.com-username
        HostName github.com
        User git
        IdentityFile ~/.ssh/username

Host bitbucket.org
        HostName bitbucket.org
        User git
        IdentityFile ~/.ssh/bitbucket

Host gitlab.com
        HostName gitlab.com
        User git
        PreferredAuthentications publickey
        IdentityFile ~/.ssh/gitlab
  1. Optional. Copy SSH config into each environment
cp -b ~/.ssh/config /mnt/c/Users/%USERPROFILE%/.ssh/
  1. Set up autostarting ssh-agent
    1. Update %USERPROFILE%/.profile on the next way using:
      nano .profile
    2. In the end of the file, put the next
    if [ "$BASH" ]; then
    if [ -f ~/.bashrc ]; then
    . ~/.bashrc
    eval "$(ssh-agent -s)"
    fi
    fi
    
    mesg n 2> /dev/null || true

Cloning repositories using different accounts

By default,

git clone git@github.com:organization/remote-repository.git
git clone git@bitbucket.org:organization/remote-repository.git
git clone git@gitlab.com:organization/remote-repository.git

For specific account,

git clone git@github.com-username:organization/remote-repository.git
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment