Cloning a Repository from GitHub or GitLab

Summary: Use git clone to copy a remote repository locally.


When collaborating on software projects or exploring open source code, you often need to copy a project from a hosting platform like GitHub or GitLab to your local machine. This process is known as cloning a repository. In this guide, you’ll learn how to clone repositories from GitHub or GitLab using the git clone command.


What Does "Cloning" Mean in Git?

Cloning is the process of creating a local copy of a remote repository. This local copy contains the project’s code, history, branches, and tags, allowing you to:

  • Browse and modify code offline
  • Make experimental changes safely
  • Contribute to the project through pull/merge requests

Prerequisites

Before cloning, ensure:

  1. Git is installed on your computer.
    Download Git if you haven’t already.
  2. Access to the repository you wish to clone (public or private, with proper authentication if required).

Finding the Clone URL

Both GitHub and GitLab provide a URL for each repository, which you can use with git clone:

  • HTTPS URLs:
    • Example (GitHub): https://github.com/user/repo.git
    • Example (GitLab): https://gitlab.com/user/repo.git
  • SSH URLs:
    • Example (GitHub): git@github.com:user/repo.git
    • Example (GitLab): git@gitlab.com:user/repo.git
  • Which to use?
    • Use HTTPS for simplicity.
    • Use SSH for extra security and convenience (if you have SSH keys set up).

Where to Find the URL

  1. On GitHub:

    • Go to the repository page.
    • Click the green Code button.
    • Under “Clone,” copy the chosen URL.
  2. On GitLab:

    • Go to the repository page.
    • Click the blue Clone button/dropdown.
    • Copy the HTTP or SSH URL.

Cloning a Repository: Step-by-Step

1. Open a Terminal

Open your terminal or command prompt. Navigate to the directory where you want the cloned project to reside.

cd /path/to/your/projects/

2. Run the git clone Command

Use the appropriate command, replacing URL with your copied repository URL.

git clone URL

Examples

  • Cloning a GitHub repository via HTTPS:

    git clone https://github.com/octocat/Hello-World.git
    
  • Cloning a GitLab repository via SSH:

    git clone git@gitlab.com:username/project.git
    

3. Wait for the Clone to Complete

Git will copy the repository. When complete, you’ll see a new folder (named after the repo) in your current directory.

ls
# Output:
# Hello-World/
# or
# project/

4. Start Working

Enter the new directory:

cd Hello-World
# or
cd project

Now you can explore the files, create branches, make changes, and interact with Git as needed.


Cloning Private Repositories

If a repository is private:

  • HTTPS: You’ll be prompted for your username and password or a personal access token.
  • SSH: Ensure your public SSH key is added to your GitHub or GitLab account.

Additional git clone Options

  • Clone to a specific folder name:

    git clone URL my-folder
    
  • Clone a specific branch only:

    git clone --branch branch-name URL
    
  • Clone with less history (shallow clone):

    git clone --depth=1 URL
    

Common Issues

  • Authentication errors: Double-check credentials, SSH keys, and permissions.
  • Repository not found: Check if the URL is correct.
  • Network errors: Verify your internet connection or proxy settings.

Conclusion

Cloning a repository is the first step to working with remote code and collaborating with teams. With the simple git clone command, you can quickly set up a local development environment from any project hosted on GitHub or GitLab. Happy coding!


Further Reading: