Note: GitHub (and others) require that you to set up different SSH keys for each separate GitHub account and each computer.
Note: The following example assumes you are using GitHub as your remote repository hosting service. You can perform most of these operations in GitLab and other services.
You can download the setup script at this link.
git
and ssh
are installed on your computer.Open Git Bash or your terminal after installation.
Make a SSH directory if it does not exist.
SSH_DIR=~/.ssh
if [ -d "$SSH_DIR" ]; then
echo "$SSH_DIR exists."
else
echo "$SSH_DIR does not exist, making .ssh directory now."
mkdir -p $SSH_DIR
fi
SSH_CONFIG_FILE=~/.ssh/config
if [ -f "$SSH_CONFIG_FILE" ]; then
echo "$SSH_CONFIG_FILE exists."
else
echo "$SSH_CONFIG_FILE does not exist, making config file now."
touch $SSH_CONFIG_FILE
fi
mkdir -p ~/.ssh/github
Note:
github
with your remote repo service.# replace <username> with your GitHub username
GITHUB_USERNAME=<username>
# (Optional) You can use the default "HOSTNAME" environment variable or redefine <hostname> to something that reminds you of this computer (e.g., "home" for home computer, "work" for work computer, etc.)
HOSTNAME=<hostname>
# Use this if you want to login using only password/passphrase
ssh-keygen -t ed25519 -C "${HOSTNAME}_gh_${GITHUB_USERNAME}" -f ~/.ssh/github/gh_${GITHUB_USERNAME}
# OR
# Use this if you want to login with a physical hardware key (e.g., Yubikey)
# With an option to authenticate password/passphrase (like 2FA)
ssh-keygen -t ed25519-sk -C "${HOSTNAME}_gh_${GITHUB_USERNAME}" -f ~/.ssh/github/gh_${GITHUB_USERNAME}
Notes:
~/.ssh/github/gh_${GITHUB_USERNAME}
- private SSH key (without file extension)~/.ssh/github/gh_${GITHUB_USERNAME}.pub
- public SSH key (with .pub
file extension)printf "Host gh_${GITHUB_USERNAME}\n\tHostname github.com\n\tIdentityFile ~/.ssh/github/gh_${GITHUB_USERNAME}\n\n" >> ~/.ssh/config
GITHUB_USERNAME
environment variable is set up correctly for every SSH private key you are adding to the SSH agent.# replace <username> with your GitHub username
GITHUB_USERNAME=<username>
eval $(ssh-agent -s)
# Repeat for all SSH private keys of every GitHub account you have set up
ssh-add ~/.ssh/github/gh_${GITHUB_USERNAME}
ssh-agent
run automatically when you open your Git Bash/terminal.# Add "ssh-agent" command to your ".bashrc" file (only needs to be done ONCE PER MACHINE)
echo "eval \$(ssh-agent -s)" >> ~/.bashrc
ssh-add
run automatically when you open your Git Bash/terminal.# Add "ssh-add" command to your ".bashrc" file (need to be done ONCE PER GITHUB ACCOUNT)
echo "ssh-add ~/.ssh/github/gh_${GITHUB_USERNAME}" >> ~/.bashrc
GITHUB_USERNAME
environment variable is set up correctly for every SSH public key you are about add to different GitHub accounts.# replace <username> with your GitHub username
GITHUB_USERNAME=<username>
cat ~/.ssh/github/gh_${GITHUB_USERNAME}.pub
Sign in to your GitHub account.
Click on your profile picture on the top right. Click on Settings
.
Click on SSH and GPG keys
under the Access group on the left side of the settings.
Click on the button called New SSH key
.
Paste in the SSH public key in the Key
text box. Leave Title
text box empty and all other settings the same.
Click Add SSH key
.
(Optional) If you are setting up different computers (with different SSH keys) to the same GitHub account, repeat Steps 0-7 for every computer you want to add to your GitHub account. Make sure GITHUB_USERNAME
is the same for every computer.
(Optional) If you have multiple GitHub accounts, repeat Steps 0-7 on the same machine. Make sure GITHUB_USERNAME
is the different after you completed adding the keys for each account.
These steps assumed that the SSH agent is running, and you have added the SSH key for your account to the SSH agent. Please refer to the previous section.
Grab the SSH link for your GitHub repo. It should look like: [email protected]:<username>/<repo_name>.git
Replace github.com
from step 1 with gh_${GITHUB_USERNAME}
when cloning your repo.
git clone git@gh_<username>:<username>/<repo_name>.git
cd <repo_name>/
git
username and email.# Do only ONCE per computer
git config user.name --global ""
git config user.email --global ""
<email>
with either your Github email1 or the private GitHub commit email 2# Do for each repo per computer
git config user.name <username>
git config user.email <email>
Notes:
1 Be sure to replace <email>
with your GitHub’s email.
2 For additional security, GitHub allows you to hide your email and provides you with a commit email address. You can use this provided email, rather than the actual GitHub email.
cd <path/to/repo>/
Open the .git/config
inside your repository using any text editor
Locate the line: url = [email protected]:<username>/<repo_name>.git
Replace github.com
with gh_<username>
. This line will now look like url = git@gh_<username>:<username>/<repo_name>.git
Save the configuration file.