How to clone a Git repository

· Category: Git

Short answer

Run git clone <url> to download a full copy of a remote repository to your local machine.

Steps

  1. Clone via HTTPS:
git clone https://github.com/user/repo.git
  1. Clone via SSH:
git clone [email protected]:user/repo.git
  1. Clone into a specific folder:
git clone https://github.com/user/repo.git my-folder
  1. Clone a specific branch:
git clone --branch main --single-branch https://github.com/user/repo.git

Tips

  • SSH cloning requires your public key to be added to the remote server.
  • Use --depth 1 for a shallow clone if you only need the latest commit.
  • git clone --recursive also clones submodules.

Common issues

  • Authentication failures: check your SSH key or personal access token.
  • Large repositories: shallow clones or sparse checkouts can save time and disk space.