How to Clone a Git Repository
Copy a remote Git repository to your computer with git clone, choose between HTTPS and SSH URLs, pick the folder name, and start working in it.
You found a repository on GitHub, GitLab, or another host and want a local copy with its full history. git clone downloads it in one command.
Copy the repository URL
On GitHub, open the repository page and click the green Code button. Pick either the HTTPS or SSH tab and copy the URL.
HTTPS looks like https://github.com/user/repo.git. It works everywhere and asks for credentials when you push to a private repository.
SSH looks like git@github.com:user/repo.git. It requires an SSH key added to your account but never prompts for a password afterward.
If you are not sure, use HTTPS. The choice can be changed later with git remote set-url origin.
Run git clone
git clone https://github.com/user/repo.git
Git creates a folder named after the repository (repo in this case), downloads every commit, and checks out the default branch.
Cloning into 'repo'...
remote: Enumerating objects: 120, done.
Receiving objects: 100% (120/120), done.
Choose the folder name
By default the folder takes the repository's name. To use a different one, add it as a second argument:
git clone https://github.com/user/repo.git my-project
The folder must not already exist, or Git refuses to clone into it.
Move into the folder
cd repo
The clone already has origin set as the remote, so git pull, git push, and git branch all work immediately. Run git log --oneline to see the history you downloaded.
For large repositories. Add --depth 1 to download only the latest commit instead of the full history: git clone --depth 1 URL. The clone is much smaller and faster. Fetch the rest later with git fetch --unshallow if you need it.
If cloning fails with a permission error. Private repositories need authentication. For HTTPS, sign in when prompted (GitHub requires a personal access token, not your account password). For SSH, make sure the key on this machine is added to your account.
More Coding how-tos
- How to Add a .gitignore File
- How to Check Which Node.js Version Is Installed
- How to Check Which Python Version Is Installed
- How to Create a New Git Branch
- How to Create a Python Virtual Environment
- How to Install Node.js with nvm
- How to Make a Script Executable in the Terminal
- How to Print Hello World in JavaScript
- How to Print Hello World in Python
- How to Run a Local Web Server in One Command
- How to Undo the Last Git Commit