Ways to Connect with Remote GitHub Repository From Local Machine
Hey thereππ»! This article is all about GitHub SSH and HTTPS URLs which are used in order to perform Git actions from our local machines π».
Intoduction
We'll learn about the different ways GitHub provides for cloning a repository so that we can easily interact with it from our local machines using Git Bash or terminal. We will mainly talk about the SSH and the HTTPS way.
GitHub Clone URLs
To start communicating with a remote git repository from a local machine, you need to first clone
it using git clone <REMOTE_URL>
to make the repository available on your local to start working with it. GitHub provides us with the two options for <REMOTE_URL>
: HTTPS and SSH.
An HTTPS URL example
https://github.com/....
An SSH URL example
git@github.com/....
GitHub also provides the third option for cloning using GitHub CLI. If you are using GitHub CLI then this doc would help you.
To see the URLs you can navigate to the main page of the repository, click Code and you will see all the three options.
Cloning with HTTPS URL
If you want to use an HTTPS clone URL, It will always ask you for your GitHub credentials i.e. username and password (hence it's a password-based authentication) to authenticate each action you want to perform like git push
, git pull
, git fetch
etc.
GitHub has removed Password-based authentication in favor of more secure authentication methods. Read here and here but we can use Personal Access Token (PAT) instead of our current GitHub password. Refer to this doc for creating PAT. Anyway we still need to enter it for every interaction.
To avoid being prompted for your password you can cache your credentials by configuring Git with Git Credential Manager (GCM) or using GitHub CLI to cache your credentials. Git automatically uses the cached PAT when you pull or push a repository using HTTPS.
Refer this for - Caching your GitHub credentials in Git
Cloning with SSH URL
An SSH key is a pair of public and private keys. To generate them we will use the ssh-keygen
command. Both the keys reside in our system and we only add the public key to our GitHub account to authenticate. It's a token-based authentication.
1. Generate SSH Key
The default location where the keys reside is ~/.ssh
. To see the keys we can execute this command on the terminal or Git Bash -> ls -al ~/.ssh
(you may also use file manager). If you receive an error that ~/.ssh
doesn't exist, it is because you do not have an existing SSH key pair in the default location and you need to generate one. File with name id_ed25519
(which may be different) and extension .pub
is a public key and file name id_ed25519
with no extension is a private key.
- Open the terminal (If you're using Windows, use Git Bash)
- Generate keys using
ssh-keygen
command$ ssh-keygen -t ed25519 -C "your_email@example.com
t
- which encryption algorithm (dsa
, ecdsa
, ecdsa-sk
, ed25519
, ed25519-sk
, rsa
) we want to use to encrypt the keys.
C
- Stands for comment. Whatever we provide, the value will be appended at the end of the public key. It can be anything. The purpose is to identify the keys (we can have pairs of ssh keys on one machine).
- If you're prompted to enter a file in which you want to save the key, press
Enter
. This accepts the default file location which is~/.ssh
. - Next, You'll be asked to add a passphrase. It is optional. If you want to add an extra layer of security then you can add a passphrase to your SSH key pair. You will be asked to enter the passphrase every time you try to communicate with a remote repository. But it is possible to save it using
ssh-agent
which will remember your passphrase. If you don't want then press Enter.
Passphraseless authentication will not ask you for your credentials for every push or pull on a repository.
2. Add Public SSH Key to GitHub Account
- Copy the public SSH key
You can have a different name for the file. Change it accordingly.
$ cat ~/.ssh/id_ed25519.pub # you can see the value
- On your GitHub account, click your profile photo (upper-right corner of any page), then click Settings.
- In the Access section of the sidebar, click SSH and GPG keys.
- Click New SSH key.
- In the Title field, add a descriptive label for the new key. And paste your key into the "Key" field.
- Click Add SSH key. If prompted, confirm your GitHub password.
Thanks for reading the articleππ»! If you liked it, please do share it π