Cloning is like borrowing a book from a library, except in addition to the book, you are taking the entire history of its writing, all previous versions, and the author's notes. When you clone a Git repo, you download much more than files.
The basic clone
The simplest command to copy an existing repositoryWhat is repository?A project folder tracked by Git that stores your files along with the complete history of every change, inside a hidden .git directory.:
# Clone a repository from GitHub
git clone https://github.com/user/my-project.git
# This creates a 'my-project' folder with all content
cd my-projectYou now have a complete copy of the project on your computer, ready to be modified.
Clone into a custom folder
By default, Git creates a folder with the repositoryWhat is repository?A project folder tracked by Git that stores your files along with the complete history of every change, inside a hidden .git directory. name. But you can choose a different name:
# Clone into a folder named 'client-work'
git clone https://github.com/user/my-project.git client-work
# Now it is in 'client-work/', not 'my-project/'
cd client-workThis is useful when working on multiple versions of the same project or when you want a more descriptive name.
The different URL protocols
Git supports several protocols to connect to remotes. Each has its advantages.
| Protocol | URL format | Best for |
|---|---|---|
| HTTPS | https://github.com/user/repo.git | Quick setup, works behind firewalls |
| SSH | git@github.com:user/repo.git | No password after setup, more secure |
| GitHub CLI | gh repo clone user/repo | Modern alternative with extra features |
HTTPSWhat is https?HTTP with encryption added, so data traveling between your browser and a server can't be read or tampered with by anyone in between. (the simplest)
git clone https://github.com/user/repo.gitWorks everywhere, no configuration necessary. Since 2021, GitHub requires Personal Access Tokens instead of passwords, create one in your GitHub settings.
SSH (the most secure)
git clone git@github.com:user/repo.gitRequires a one-time setup:
# Generate an SSH key pair
ssh-keygen -t ed25519 -C "your-email@example.com"
# Copy the public key and add it in GitHub Settings > SSH Keys
cat ~/.ssh/id_ed25519.pubWhat happens when you clone
When you run git clone, Git performs several steps:
- Creates a new folder with the repositoryWhat is repository?A project folder tracked by Git that stores your files along with the complete history of every change, inside a hidden .git directory. name
- Initializes a Git repository (
git initimplicitly) - Adds the remote
originpointing to the source URL - Downloads all Git objects (commits, trees, blobs)
- Creates remote-tracking branches (
origin/main,origin/develop, etc.) - Checks out the default branch (usually
main)
After the clone: first steps
# Enter the project folder
cd my-project
# Verify the configured remote
git remote -v
# origin https://github.com/user/repo.git (fetch)
# origin https://github.com/user/repo.git (push)
# See all branches (local and remote)
git branch -a
# * main
# remotes/origin/HEAD -> origin/main
# remotes/origin/main
# remotes/origin/feature-loginWhen you see remotes/origin/main, it is a local reference that represents the current state of main on the server. It updates when you fetch, but never automatically changes your local branch.
Shallow and partial clones
For huge repositories with years of history, you can clone selectively:
# Clone only the last 10 commits
git clone --depth 10 https://github.com/user/repo.git
# Clone only a specific branch
git clone --branch feature-login --single-branch https://github.com/user/repo.gitgit blame on old code will not work.Clone a forkWhat is fork?A personal copy of someone else's repository on a platform like GitHub, letting you make changes freely and propose them back via pull request. vs the original
When contributing to open source, you generally clone your fork, not the original:
# Wrong: clone the original project (you cannot push here)
git clone https://github.com/facebook/react.git
# Right: clone YOUR fork (you can push freely)
git clone https://github.com/YOUR-USERNAME/react.gitYour fork is under your control. You push your branches to it, then create pull requests to the original project.
Quick reference
| Command | Description |
|---|---|
git clone url | Standard clone into a folder with the project name |
git clone url folder | Clone into a specific folder |
git clone --branch name --single-branch url | Clone only one branch |
git clone --depth N url | Shallow clone (last N commits) |
git clone --recursive url | Clone with submodules |