how to connect to github repository from terminal

How to Connect to GitHub Repository from Terminal

By Matthew Wilson
AD

To establish a connection between your local terminal and a GitHub repository, you'll primarily use SSH (Secure Shell) or HTTPS. Here's a comprehensive guide on connecting to a GitHub repository from your terminal via both these protocols.

First, it's crucial to understand that Git is a distributed version control system, enabling multiple developers to work together on non-linear projects. Connecting your terminal to a GitHub repository allows you to clone, commit, pull, and push changes seamlessly.

SSH Connection

Using SSH offers more security and convenience as it eliminates the need to enter your GitHub password every time you push or pull changes.

AD
How to create repository on GitHub - The Tech Edvocate
How to create repository on GitHub - The Tech Edvocate
How to fork repository on GitHub - The Tech Edvocate
How to fork repository on GitHub - The Tech Edvocate
How to use Terminal for productivity
How to use Terminal for productivity

First, ensure you have an SSH key pair generated on your local machine. If not, generate one using the following command in your terminal:

Generate SSH Key Pair

Open your terminal and paste the following command, substituting 'your_email@example.com' with your actual email:

How to Delete a GitHub Repository
How to Delete a GitHub Repository
Building a Math Application with LangChain Agents | Towards Data Science
Building a Math Application with LangChain Agents | Towards Data Science

ssh-keygen -t ed25519 -C "your_email@example.com"

Store the generated files in the default location (~/Documents/.ssh/id_ed25519 and ~/Documents/.ssh/id_ed25519.pub).

AD

Add SSH Key to GitHub

Next, add the generated public key to your GitHub account:

Copy the contents of the public key file (id_ed25519.pub):

cat ~/.ssh/id_ed25519.pub

AD

Paste the copied key into the 'New SSH key' field on your GitHub account's SSH keys settings page (Settings > SSH keys). Provide a title for the key and click 'Add key.'

Now, test the connection by running this command in your terminal:

ssh -T git@github.com

AD

You should see a message indicating that you've successfully authenticated to GitHub.

Finally, clone the repository using SSH. Replace 'your_username' and 'repo_name' with the respective values:

git clone git@github.com:your_username/repo_name.git

HTTPS Connection

Using HTTPS requires authentication every time you push or pull changes. However, it's suitable if you prefer not to use SSH.

To clone a GitHub repository using HTTPS, use the following command, replacing 'your_username' and 'repo_name' with the actual values:

Clone Repository

git clone https://github.com/your_username/repo_name.git

Once cloned, navigate into the repository's directory and add the remote URL:

git remote add origin https://github.com/your_username/repo_name.git

Push/clone with PAT (Personal Access Token)

To avoid entering your GitHub password every time you push changes, generate a Personal Access Token (PAT):

1. Go to your GitHub account's settings, click 'Developer Settings', then 'Personal access tokens'.

2. Click 'Generate new token', give it a name, and select the required scopes (repo for public repositories or repo:status for private repos).

3. Copy the generated token.

Replace 'your_token' in the following command with your copied PAT:

git remote set-url origin https://your_token@github.com/your_username/repo_name.git

Now, you're set to clone, commit, pull, and push changes to your GitHub repository using either SSH or HTTPS. Happy coding!

Remember to keep your SSH private key and PAT secure. Never expose them publicly or share them unnecessarily. This guide has equipped you with the necessary skills to connect your terminal to a GitHub repository, enabling seamless collaboration and efficient project management.

AD