git clone is the command for copying (cloning) a remote repository to your computer.
What Is Cloning?
Cloning creates a full copy of a repository:
- ✅ All files
- ✅ Full commit history
- ✅ All branches
- ✅ All tags
Basic Usage
Syntax
git clone <URL> [folder]
Usage Examples
Basic usage
git clone https://github.com/username/repo.git
Creates a repo/ folder with the contents.
Clone into a specific folder
git clone https://github.com/username/repo.git my-folder
Creates my-folder/ instead of repo/.
Clone a specific branch
git clone -b develop https://github.com/username/repo.git
Clones the develop branch instead of main.
URL Types
HTTPS (recommended for beginners)
git clone https://github.com/username/repo.git
Pros:
- ✅ Easy to set up
- ✅ Works everywhere
- ✅ Works through firewalls/proxies
Cons:
- ❌ Need to enter a password/token on push
SSH (for advanced users)
git clone git@github.com:username/repo.git
Pros:
- ✅ No password on push (uses SSH keys)
- ✅ More secure
Cons:
- ❌ Requires SSH key setup
GitHub CLI
gh repo clone username/repo
If GitHub CLI is installed.
Cloning via GitHub Desktop
Method 1: From your repository list
- File → Clone Repository
- GitHub.com tab
- Select the repository
- Choose a folder
- Clone
Method 2: By URL
- File → Clone Repository
- URL tab
- Paste the repository URL
- Choose a folder
- Clone
Method 3: From the GitHub website
- On the repository page click Code
- Open with GitHub Desktop
- Confirm in the dialog
What Happens During Cloning?
git clone https://github.com/username/repo.git
Git performs:
- Creates the
repo/folder - Initializes Git (
.gitfolder) - Adds the
originremote (link to GitHub) - Downloads all objects (commits, trees, blobs)
- Checks out the main branch (usually
main)
Result:
repo/
├── .git/ # Git database
├── README.md
├── src/
└── ...
Useful Options
Shallow clone
Download only the latest commit:
git clone --depth 1 https://github.com/username/repo.git
Why:
- Faster (less data)
- Saves space
- Full history not needed
When to use:
- CI/CD pipelines
- Temporary use
- Very large repositories
Clone without checkout
git clone --no-checkout https://github.com/username/repo.git
Downloads the data but doesn’t create working files.
Clone with submodules
git clone --recursive https://github.com/username/repo.git
If the project uses Git submodules.
Common Issues and Next Steps
“fatal: could not read Username”
Cause: Repository is private or the URL is wrong.
Fix:
1. Check the URL
2. Make sure you’re logged in to GitHub
3. For private repos — use HTTPS with a token or SSH
“fatal: destination path ‘repo’ already exists”
Cause: The folder already exists.
Fix:
1. Delete the folder: rm -rf repo/
2. Or clone into another folder: git clone ... other-folder
Very slow cloning
Cause: Large repository or slow internet.
Fix:
1. Use --depth 1 (shallow clone)
2. Check your internet speed
3. Try again later (GitHub servers may be under load)
“Permission denied (publickey)”
Cause: SSH key is not configured.
Fix:
1. Use HTTPS instead of SSH
2. Or set up SSH keys: https://docs.github.com/en/authentication/connecting-to-github-with-ssh
After Cloning
Check the remote
cd repo
git remote -v
Output:
origin https://github.com/username/repo.git (fetch)
origin https://github.com/username/repo.git (push)
View branches
git branch -a
Switch to another branch
git checkout develop
Update the code
git pull
Fork vs Clone
Clone
A copy of the repository for reading/working.
git clone https://github.com/someone/repo.git
You cannot push to the original.
Fork
A copy of the repository for your own changes.
- Click Fork on GitHub
- Clone YOUR fork:
bash git clone https://github.com/YOUR-username/repo.git
You can push to your fork!
Best Practices
✅ Clone into an organized directory structure:
~/Projects/
├── personal/
├── work/
└── learning/
✅ Use HTTPS to start (simpler)
✅ After cloning:
1. Read README.md
2. Install dependencies
3. Create your own branch for work
✅ For large repositories use --depth 1
Now you’re a git clone master! Clone and learn from others’ code! 📦
💬 Comments (0)
No comments yet
Be the first to share your opinion about this article!