iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

git clone

git clone copies a remote repository to your machine: history, branches, tags, and a working tree. Knowing the flags (shallow, blobless, partial, sparse) saves bandwidth and disk on huge repos.

clone, depth, sparse, partial, mirror

EXAMPLE
# 1) Basic
git clone https://github.com/me/my-app.git
git clone git@github.com:me/my-app.git
git clone https://github.com/me/my-app.git my-folder       # custom dir name

# 2) Branch / tag
git clone --branch develop https://github.com/me/my-app.git
git clone --branch v1.2.3 --single-branch https://github.com/me/my-app.git
# --single-branch saves bandwidth — fetches only that branch's history.

# 3) Shallow clone — only the most recent commits
git clone --depth=1 https://github.com/torvalds/linux.git
# Huge repos -> minutes instead of hours. You CAN'T look at older history without fetching.
# Convert to full later: git fetch --unshallow

# 4) Blobless / treeless clones (Git 2.19+) — partial clone
git clone --filter=blob:none   https://github.com/me/my-app.git    # download blobs on demand
git clone --filter=tree:0       https://github.com/me/my-app.git    # only the commit graph
# Combine with sparse-checkout for huge monorepos.

# 5) Sparse checkout — only some directories in the working tree
git clone --filter=blob:none --no-checkout https://github.com/me/monorepo.git
cd monorepo
git sparse-checkout init --cone
git sparse-checkout set apps/web packages/shared
git checkout main
# Working tree only contains the listed paths. The rest of the repo is still queryable.

# 6) Mirror clone — for backups / migrations
git clone --mirror https://github.com/me/my-app.git
# Bare repo with all refs (branches, tags, remotes). Use for serving:
cd my-app.git
git push --mirror git@new-host:me/my-app.git

# 7) Bare clone — like mirror but ignores remote-tracking refs
git clone --bare https://github.com/me/my-app.git
# Used as the canonical repo on a server.

# 8) With submodules
git clone --recurse-submodules https://github.com/me/my-app.git
# Or after the fact:
git submodule update --init --recursive

# 9) SSH vs HTTPS
# • SSH (git@github.com:...) — uses SSH keys; no password prompts
# • HTTPS (https://...) — uses credential helper; works behind corporate proxies
# • Switch a clone:
git remote set-url origin git@github.com:me/my-app.git

# 10) Speeding up clones
# • --depth=1                    — shallow
# • --filter=blob:none          — partial blob
# • --jobs=4                      — parallel transport (Git 2.25+)
# • Use a closer mirror or cache (artifact registry, proxy)
# • GitHub: use codeload.github.com for archive downloads when history isn't needed

# 11) After clone — first-time setup
cd my-app
git config user.email mara@example.com
git config user.name 'Mara Example'
git branch -M main
# Initialize hooks, install deps, link to issue tracker if your team does that.

# 12) Cloning from different protocols
git clone https://github.com/...                  # HTTPS
git clone git@github.com:...                       # SSH
git clone file:///srv/git/repo.git                  # local on same machine
git clone /srv/git/repo.git                          # local path (hardlinks possible)
git clone ssh://user@host:22/srv/git/repo.git       # explicit SSH URL

# 13) Authentication
# • Personal access tokens (GitHub PATs) — use 'fine-grained' tokens per-repo
# • SSH keys (ed25519 preferred)
# • Credential helpers: osxkeychain (Mac), wincred (Win), libsecret (Linux), gh CLI
git config --global credential.helper osxkeychain

# 14) Cleanup + cancel
# Interrupt a clone (Ctrl+C). Remove the partial dir; retry.
# 'fatal: clone of foo into 'bar' failed' → check disk space, partial dir, and network.

# 15) GitHub Codespaces / Replit / Cloud IDEs — clone is hidden but happening
# All git operations work the same; you just don't run 'clone' yourself.

# 16) Common bugs
# • Cloned over HTTPS but team uses SSH → git push asks for password; switch remote URL
# • Forgot --recurse-submodules → submodule directories are empty
# • Shallow clone + later need history → 'git fetch --unshallow'
# • Clone of huge repo eats disk → use --filter=blob:none + sparse-checkout
# • 'remote HEAD refers to nonexistent ref' → upstream renamed default branch; git remote set-head origin -a
# • Cloning a private repo via HTTPS without a token → 401; configure credential helper
# • Wrong line endings on Windows after clone → set core.autocrlf=input globally
# • LFS files showing as pointers → install Git LFS, then 'git lfs pull'

Why it matters

git clone is fast for small repos, slow for everything else — reach for --depth=1, --filter=blob:none, and sparse checkout when working with huge mono-repos. Switch HTTPS to SSH for daily work, configure your name and email immediately, and pull submodules with --recurse-submodules instead of forgetting later.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
git clone https://github.com/user/repo.git
git clone --depth 1 https://github.com/user/big-repo.git   # shallow
Try it Yourself »

Exercise

Copy a remote repo to your machine.

git https://github.com/user/repo.git

Discussion

Loading…