git clone
Downloading repositories with git clone — shallow clones, depth limits, and clone workflows.
What you'll learn
- How to clone a repository and what gets downloaded
- The difference between HTTPS and SSH clone URLs
- Cloning into a custom directory name
- Shallow clones with --depth to save time and space
Concept
What git clone Does
git clone downloads a complete copy of a remote repository to your local machine — including the entire commit history, all branches, and all tags. It also automatically sets up a remote called origin pointing back to the source URL, so you can push and pull.
git clone https://github.com/user/repo.git
After cloning, you have a full-fledged local repository. You can commit, branch, view history, and push changes back — everything works offline except syncing with the remote.
HTTPS vs SSH URLs
You can clone using either protocol:
# HTTPS — uses username + personal access token
git clone https://github.com/user/repo.git
# SSH — uses your SSH key pair (no password prompts)
git clone git@github.com:user/repo.git
HTTPS is easier to set up but asks for credentials. SSH is more convenient after a one-time key setup and is the recommended approach for frequent contributors. Use the SSH Key Generator tool to create a key pair and add it to your GitHub account.
Cloning into a Custom Directory
By default, git clone creates a directory named after the repository. You can specify a different name:
git clone https://github.com/user/repo.git my-custom-folder
Shallow Clones
Large repositories with years of history can take a long time to clone. A shallow clone downloads only the most recent commits instead of the full history:
# Clone only the last commit
git clone --depth 1 https://github.com/user/repo.git
# Clone the last 50 commits
git clone --depth 50 https://github.com/user/repo.git
This is useful for CI pipelines or when you only need the latest code. You can later deepen the history with git fetch --unshallow.
What Gets Set Up Automatically
When you clone, Git automatically:
- Creates the local directory and copies all files
- Sets up the
.gitfolder with full history - Creates a remote named
originpointing to the source URL - Checks out the default branch (usually
main) - Sets up branch tracking (
local maintracksorigin/main)
You can verify this with git remote -v and git branch -vv.
Example
# Clone a public repository (HTTPS)
git clone https://github.com/octocat/Hello-World.git
cd Hello-World
# Check what was set up automatically
git remote -v # shows origin pointing to the source
git log --oneline -5 # the full history is here
git branch -vv # shows tracking branch
# Clone using SSH (after setting up an SSH key)
git clone git@github.com:username/my-project.git
# Clone into a differently-named folder
git clone https://github.com/user/repo.git my-local-copy
# Shallow clone — only the latest commit (fast, small)
git clone --depth 1 https://github.com/user/huge-repo.git
# Clone a specific branch only
git clone --branch develop https://github.com/user/repo.git
# Later, deepen a shallow clone if you need full history
git fetch --unshallow
Covers the standard clone, SSH clone, custom directory naming, shallow clones for large repos, cloning a specific branch, and deepening a shallow clone. After cloning, git remote -v confirms the origin remote was set up automatically.
Try it
- SSH Key Generator
Generate an SSH key pair for cloning via SSH instead of HTTPS.
- .gitignore Generator
After cloning, generate a .gitignore for any new files you add.
Common mistakes
The mistake
Cloning into a directory that already exists and is not empty
The fix
git clone creates a new directory. If the target directory already exists and is not empty, the clone fails. Either choose a different name, or clone into an empty directory. You can also clone into the current directory with git clone <url> . if it is empty.
The mistake
Using HTTPS and being prompted for a password every time
The fix
GitHub no longer accepts account passwords for Git operations. Generate a Personal Access Token (PAT) and use it as your password, or switch to SSH by generating a key and adding it to your GitHub account.
Related Resources
Related Snippets
- Git Remote Snippet
Managing remotes after cloning.
Related Cheatsheets
- Git Cheatsheet
git clone options and remote setup commands.
Practice git clone
6 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.