Basic Git Series: How to Clone a Repository

Adam Sturge | Aug 1, 2023 min read

Overview

Cloning a Git repository creates a local copy of a remote repository on your machine. This allows you to work with the project files locally.

Steps

  1. Find the repository URL you want to clone (typically found on GitHub, GitLab, etc.)
  2. Open your terminal or command prompt
  3. Navigate to the directory where you want to store the repository
  4. Run the clone command:
git clone [email protected]:username/repository.git
  1. Wait for the download to complete

SSH Authentication

When cloning repositories using SSH, you’ll need to add your public SSH key to your GitHub account:

  1. Generate an SSH key if you don’t have one already
  2. Add your public SSH key to your GitHub account
  3. For detailed instructions, refer to the GitHub documentation on adding a new SSH key

Common Issues

  • Permission denied error: Make sure you have the proper access to the repository and your SSH key is correctly set up
  • Slow download: Large repositories may take time to clone; consider using a shallow clone for faster download:
    git clone --depth=1 [email protected]:username/repository.git
    

Additional Notes

  • To clone a specific branch, use the -b flag:
    git clone -b branch-name [email protected]:username/repository.git
    
  • If you prefer HTTPS over SSH, you can use the HTTPS URL:
    git clone https://github.com/username/repository.git