This article explains how to clone your git repo with SSH on Linux.
Authentication
Don’t forget that you will still need to authenticate with git by running:
git config --global user.name "Your Name" git config --global user.email "you@example.com"
1. generate an ssh key
Paste this command in your terminal
ssh-keygen -t ed25519 -C "your_email@example.com"
- You’ll be prompted to choose a file to save the key (default is ~/.ssh/id_ed25519)
- Press Enter to accept the default path.
- You can also set a passphrase for extra security. (or press enter for no passphrase) And you’ll also get a funy artwork :3
2. Add the SSH Key to the SSH Agent
# Start ssh-agent in the background
eval "$(ssh-agent -s)"
# The actual command
ssh-add ~/.ssh/id_ed25519
3. copy the pub key
cat ~/.ssh/id_ed25519.pub
4. copy SSH Key
open ~/.ssh/id_ed25519.pub in a text editor and copy it’s contents
5. Add the SSH Key to Your Git Host (GitHub example)
- Go to GitHub → Settings → SSH and GPG keys
- Click New SSH key
- Paste your key and give it a name
- Save
5. Use SSH to Clone Repositories
- On the git repo page, under the blue or green
clone/codebutton, it is default chosenHTTPS, selectSSHinstead - Clone the link
- in your terminal, type
git clone <paste the link>and press enter
6. Use multiple accounts
run nano ~/.ssh/config. you will see something like this:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
add your new ssh below it:
# Default
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
# New account
Host github-accountname
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_accountname
IdentitiesOnly yes
Save and close (Ctrl + O, then Enter, then Ctrl + X).
And then you can clone using the alias:
git clone git@github-accountname:accountname/github_repo.git
and you can then also set your username and mail for each repo:
git config user.name "username"
git config user.email "your_new_email@example.com"
or use --global to set a default.
And you can check per repo which credentials are used:
git config user.name
git config user.email