How to clone a git repository using the command line

Key takeaways

  • Clone a Git repository to create a local copy of the project, allowing you to work on it locally, which enables tasks such as resolving merge conflicts, modifying files, and making commits.

  • Use git clone {repository URL} to create a local copy of a Git repository.

  • Choose HTTPS, SSH, or GitHub CLI for cloning based on your preferences and application security needs.

  • Copy the URL from the GitHub repository's "Code" section.

  • Resolve cloning errors by checking the Git version, repository URL, and SSH access.

Imagine having the power to instantly create a personal workspace for any Git project you’re enthusiastic about. Cloning a Git repository gives us this capability, allowing us to replicate the repository on the local computer or codespace. This not only gives us full control over the codebase but also lets us work on tasks like resolving merge conflicts, adding or removing files, or making larger commits with ease. With a local copy in hand, we can work on the project anytime, anywhere. In this Answer, we’ll explore how to clone a Git repository using the command line.

Linus Torvalds, the creator of Linux, released Git in 2005 to provide a free and open-source version control system that could efficiently handle the development needs of Linux kernel developers.

git clone command syntax

Git provides git clone command to clone a repository that is already present on GitHub from the command line.

git clone {repository URL}

This command clones all of the folders, files, commits and branches of the specified repository. We just need to replace {repository URL} with our GitHub repository link.

Cloning an empty repository

Now we’ll learn how to clone an empty repository using the command line. Follow the steps below:

  • Open GitHub and go to the main page of the GitHub repository that we want to clone.

  • We have three options under the “Quick setup” section for cloning a repository, as listed below:

    • Clone Git repository using HTTPS: By default, the HTTPS option is selected. Click the copy symbol at the end of the URL on the right-hand side.

    • Clone Git repository using SSH: For SSH transfer protocol, include the SSH certificate provided by your organization’s SSH authority, click the SSH option, and then copy the symbol at the end of the URL on the right-hand side.

    • Set up in Desktop: Click “Set up in Desktop” on the left-hand side, download “GitHub Desktop” (if not already), and follow the instructions provided by “GitHub Desktop” to complete the clone.

Clone an empty repository
Clone an empty repository
  • Open “Git BashGit Bash is a command-line interface for Git.” or terminal and change the current working directory to the location where we want the cloned directory.

  • Type git clone in the terminal, paste the URL we copied earlier, and press “enter” to create the local clone.

git clone https://github.com/XXX/proj.git

Cloning an existing repository

Now we’ll learn how to use command line cloning for an existing repository.

  • Open GitHub and go to the GitHub repository that we want to clone.

  • Click “Code,” and we’ll have three options for cloning a repository, as listed below:

    • HTTPS: By default, the HTTPS option is selected. Click the copy symbol at the end of the URL on the right-hand side.

    • SSH: For SSH transfer protocol, include the SSH certificate provided by your organization’s SSH authority, click the SSH option, and then copy the symbol at the end of the URL on the right-hand side.

    • GitHub CLI: Select the “GitHub CLI” option and click the copy symbol at the end of the URL on the right-hand side.

Click "Code" and copy the URL
Click "Code" and copy the URL

  • Open “Git Bash” or terminal and change the current working directory to the location where we want the cloned directory.
  • Type git clone in the terminal, paste the URL we copied earlier, and press “enter” to create the local clone.
git clone https://github.com/XXX/proj.git

Resolving cloning errors

We may encounter multiple errors while cloning, below is a list of some errors and possible solutions:

  • Incorrect Git version: Make sure you’re using Git version 1.7.10 or later. Update if necessary.

  • Repository not found: Make sure the repository exists and you have access permissions, and check URL spelling and case sensitivity.

  • SSH access error: In exceptional cases, you might not have the correct SSH access to a repository. Make sure your personal GitHub account is linked to the SSH key you’re using.

Hands-on practice

Hands-on practice is essential for truly understanding and mastering new concepts. Just as Donald Knuth suggested:

The only way to learn about new things is to do them; the way to learn usability is to make something usable.

To gain hands-on experience, you can clone any repository using the following terminal. Simply specify the URL of your desired public repository in the git clone command. To verify the cloned repository, use the ls command to view the contents of the directory where you cloned it.

Terminal 1
Terminal
Loading...

Let’s quickly assess your understanding of how to clone a Git repository method by the following quiz:

Quiz!

1

What is the correct syntax to clone a repository using HTTPS?

A)

git clone git@github.com:username/repo.git

B)

git clone ssh://github.com/username/repo.git

C)

git clone https://github.com/username/repo.git

D)

None of the above

Question 1 of 30 attempted

GitHub repository cloning allows developers to create a local copy of a project and work on it independently. By using the git clone command and understanding common issues, they can efficiently manage code and contribute effectively.

If you're eager to enhance your software development process and ensure the highest quality, our Learn to Code: Become a Software Engineer path is the perfect next step. With this structured path, you'll go beyond fundamental concepts like generators and dive into advanced topics, including testing techniques, test automation and design, and essential practices like deployment and Git for version control, ensuring smooth software development workflows.

Don’t just build software—become proficient and ready for the challenges of the real world.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What is version control?

Version control is a management system that allows you to record and track changes in your source code and files so that you can recall certain versions later. It’s like a Google Doc for programming, where you can collaborate with multiple people working on the same code and see the source code’s history.


What is Bitbucket?

Bitbucket is a web-based Git repository hosting platform. It allows developers to work together on code projects, review changes, and handle version control.


How do you clone a Git repository to a specific folder?

You can clone a Git repository to a specific folder using the following command:

git clone https://github.com/username/repo.git /path/to/folderdestination

What is a Git repository?

A repository (commonly referred to as repo) is a collection of source code. A repository has commits to the project or a set of references to the commits (i.e., heads).


What are the most used Git commands?

There are multiple most used commands, including:

  • git clone: This copies a repository.
  • git pull: This updates local repository with remote changes.
  • git commit: This saves our changes to the local repository.
  • git push: This uploads changes to a remote repository.

To explore more commands, visit our Answer on “What are some important git commands?”