How to Clone a Repository from GitHub

Logo_GitHub

GitHub repository can be cloned to the local machine, which creates a local copy of the repository on local computer and sync between the local computer and GitHub. This makes it easier to fix merge conflicts, add or remove files, and push larger commits. When we clone a repository, we copy the repository from GitHub.com to local machine, or to a remote virtual machine when we create a codespace. In this article we will discuss the step wise method of how to clone a GitHub repository on a local machine.

Before starting cloning make sure Git is installed on your local machine. Refer to the article to know how to install Git on Windows and Linux/Ubuntu machines.

Windows Machines

How to Install Git on Windows Machines

Linux Machines

How to Install Git on Linux Machines

Cloning Methods

There are mainly three different methods to clone a GitHub repository to a local machine. These are

  1. HTTPS
  2. SSH
  3. GitHub CLI

Here we will discuss all three methods of cloning a GitHub repository to local machine.

Since August 13, 2021 the password authentication for git cloning was removed and now we all have to use Personal Access Token [PAT] to clone git. Refer to the link to create PAT.

How to Create Personal Access Token [PAT] in GitHub

HTTPS Method

Follow the steps mentioned below to clone a GitHub repository via https method

Step1: Login
Login to the GitHub portal using username and password.

GitHub_Login_Page

Step2: Repository
Navigate to the main page of the repository you want to clone and click “Code“.

GitHub_Repository_Clone

Step3: URL
Under “HTTPS” copy the URL by clicking the icon shown below in the image.

GitHub_HTTPS_URL

Step4: Terminal or Power Shell
Open Terminal or Power Shell or command prompt.

Step5: Command
Run the command shown below and press Enter.

git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY

Step6: Confirmation
If the command is successfully committed, you will see the output as below.

edledge-Ubuntu_20.04.5_LTS:~$ git clone https://github.com/edledge/test.git
Cloning into 'test'...
sername for 'https://github.com': git@edledge.com
Password for 'https://git@edledge.com@github.com':
remote: Enumerating objects: 4461, done.
remote: Counting objects: 100% (2113/2113), done.
remote: Compressing objects: 100% (790/790), done.
remote: Total 4461 (delta 1203), reused 1665 (delta 1097), pack-reused 3248
Receiving objects: 100% (4461/4461), 1.34 MiB | 1.85 MiB/s, done.
Resolving deltas: 100% (3605/3605), done.

SSH Method

Step1: Login
Login to the GitHub portal using username and password.

GitHub_Login_Page

Step2: Repository
Navigate to the repository’s main page you want to clone and click “Code“.

GitHub_Repository_Clone

Step3: URL
Under “SSH” copy the URL by clicking the icon as shown below in the image.

GitHub_SSH_URL

Step4: Terminal or Power Shell
Open Terminal or Power Shell or command prompt

Step5: Command
Run the command shown below and press Enter.

git@github.com:YOUR-USERNAME/YOUR-REPOSITORY

Step6: Confirmation
If the command is successfully committed you will see the below message.

edledge-Ubuntu_20.04.5_LTS:~$ git clone git@github.com:edledge/test.git
Cloning into ‘test’…
Authenticity of host ‘github.com (140.82.114.3)’ can’t be established.
Are you sure you want to continue connecting (y/n/[fingerprint])? y
Resolving deltas: 100% (201/201), done. GitHub SSH clone successful.

GitHub CLI Method

GitHub CLI is an open source tool for using GitHub from your computer’s command line. When you’re working from the command line, you can use the GitHub CLI to save time and avoid switching contexts.

Step1: Login
Login to the GitHub portal using username and password.

GitHub_Login_Page

Step2: Repository
Navigate to the repository’s main page you want to clone and click “Code“.

GitHub_Repository_Clone

Step3: URL
Under “GitHub CLI” copy the URL by clicking the icon shown below in the image.

GitHub_CLI_URL

Step4: GitHub CLI
Open GitHub CLI and execute the command as shown below.

gh repo clone <REPOSITORY>

Step5: Confirmation
Below is the example of the cloning “test” repository in edledge

gh repo clone edledge/test

Other important topics

SourceKnowledge Base, Internet, Github

EA00135

Leave a Reply

Your email address will not be published. Required fields are marked *