These days it’s essential to know how to clone a GitHub repository as GitHub is the de-facto standard for source code repositories. According to this source, GitHub has 73 million users. If you follow tech blogs, such as this one, you will be asked to clone a GitHub repository to follow along. In this post, you will look into all the ways to download code from GitHub to quickly get the source code you need without setting it up on the spot.
You can pick any public repository on GitHub to follow along. In this example, I will choose one from trending repositories: Awesome Security Hardening.
Method 1: Download Zip File
If you don’t have anything on your computer, the easiest way to get the latest code from a GitHub repository is the Download Zip option.
When you land on the repository page, you will see a green button that says Code in the middle:
![Repository page showing Code button](https://cdn.devpower.co.uk/wp-content/uploads/2022/07/04050404/01-code-button-on-repository-page.png)
Click the Code button and click the Download ZIP button in the dialog:
![A dialog box is displayed after clicking the Code button, showing the Download ZIP button.](https://cdn.devpower.co.uk/wp-content/uploads/2022/07/04073009/02-download-zip-link.png)
Your download should start automatically.
Locate the downloaded file and extract:
![The downloaded zip file is extracted to a folder and contents of the folder are shown.](https://cdn.devpower.co.uk/wp-content/uploads/2022/07/04073016/03-downloaded-code-repo.png)
This example contains a single README.md file, as shown in the screenshot above.
Method 2: HTTPS
Click the Code button and copy the link shown in the HTTPS tab:
![A dialog box is displayed after clicking the Code button, showing the HTTPS tab and the link copied to the clipboard by clicking the Copy button next to the link](https://cdn.devpower.co.uk/wp-content/uploads/2022/07/04091857/04-copy-https-link.png)
In a terminal, navigate to the parent folder where you want to save the source code and run the following command:
git clone https://github.com/decalage2/awesome-security-hardening.git
You should see the results that look like this:
![A terminal window showing successful results of a git clone command](https://cdn.devpower.co.uk/wp-content/uploads/2022/07/04091858/05-git-clone-results-on-terminal.png)
Method 3: SSH
As you may have noticed in the screenshots of Method 1 and Method 2, the SSH method is not visible when you are not logged in to your GitHub account.
First, log in to your account for the SSH method to work.
Then, visit the URL of the repository and click the Code button and copy the SSH link:
![Code button clicked and dialog showing SSH tab](https://cdn.devpower.co.uk/wp-content/uploads/2022/07/04091903/06-copy-ssh-link.png)
In the terminal, run the following code:
git clone [email protected]:decalage2/awesome-security-hardening.git
The output should look like this:
![Terminal window showing successful output of git clone command using SSH method](https://cdn.devpower.co.uk/wp-content/uploads/2022/07/04091905/07-git-clone-via-ssh-results-on-terminal.png)
Method 4: GitHub CLI
If you are using GitHub frequently, I’d recommend installing GitHub CLI.
Visit cli.github.com and click the Download button on the screen:
![Landing page of GitHub CLI](https://cdn.devpower.co.uk/wp-content/uploads/2022/07/04091921/09-github-cli-main-page.png)
If you are using macOS, I’d recommend using brew to install. Brew is a package manager for macOS and Linux, and you can install it by going to brew.sh and running the installation script.
Then you can install GitHub CLI by simply running the following code in your terminal:
brew install gh
After you’ve installed the CLI, run the following command to confirm it’s installed successfully:
gh version
You should see something like this:
![Terminal window showing output of gh version command](https://cdn.devpower.co.uk/wp-content/uploads/2022/07/04091925/10-gh-version-output.png)
Once you have the CLI installed, you have to log in to your GitHub by running
gh auth login
Select the options that match your environment and circumstances using the interactive CLI. For example, in the example below, I selected GitHub.com, HTTPS and Yes to log in with GitHub credentials and selected Login with a web browser:
![Terminal window showing output of gh auth login command. Selected GitHub.com, HTTPS, GitHub credentials and login with a web browser options](https://cdn.devpower.co.uk/wp-content/uploads/2022/07/04091919/08-gh-auth-login-step-01.png)
Then, open GitHub.com in your default browser and enter the one-time code displayed on your terminal.
![GitHub webpage asking for a Device Activation code](https://cdn.devpower.co.uk/wp-content/uploads/2022/07/04091927/11-enter-authentication-code.png)
Then accept the authorization request for the CLI to access your GitHub account:
![Authorize GitHub CLI page asking for permissions for CLI to access GitHub](https://cdn.devpower.co.uk/wp-content/uploads/2022/07/04091948/12-accept-auth-request.png)
After the CLI has been installed and authorized, cloning a GitHub repository is very easy. Run the following command to clone the example repository:
gh repo clone decalage2/awesome-security-hardening
and you should see the successful results:
![Terminal window showing successful output of gh repo clone command](https://cdn.devpower.co.uk/wp-content/uploads/2022/07/04091939/13-git-clone-via-gh-cli-results-on-terminal.png)
Conclusion
You walked through all four methods to clone a GitHub repository in this article. Having the ability to clone a repository on GitHub is very important for a developer. So I hope you enjoyed this article and spent some time setting up your environment to be a more productive developer.