Introduction
git clone copies a remote repository to your machine. It creates a new directory, initializes .git inside it, fetches all objects and refs, sets up a remote called origin, and checks out the default branch. After cloning you have a complete, independent repository.
Basic clone
git clone https://github.com/torvalds/linux.git
cd linux
git remote -v
# origin https://github.com/torvalds/linux.git (fetch)
# origin https://github.com/torvalds/linux.git (push)
Choosing a directory name
By default Git uses the last path component. Override it:
git clone https://github.com/example/widget.git my-widget
Picking a protocol
- HTTPS: easiest, works through firewalls, often needs a credential helper or token.
- SSH: convenient with key auth, fast for power users.
- git://: anonymous, unauthenticated, mostly historical.
- file:// or local path: clone from another directory.
git clone [email protected]:example/widget.git
git clone /srv/git/widget.git
git clone file:///srv/git/widget.git
Useful options
git clone --depth 1 <url> # shallow: only the tip
git clone --branch v1.2.3 <url> # check out a specific branch or tag
git clone --single-branch <url> # fetch only one branch
git clone --bare <url> # bare clone, no working tree
git clone --recurse-submodules <url>
Shallow clones save bandwidth for CI; convert later with git fetch --unshallow.
What clone does, step by step
- Creates the target directory.
- Runs
git initinside it. - Adds
originas a remote. - Runs
git fetch origin. - Sets up tracking for the default branch.
- Runs
git checkoutfor that branch.
Sparse and partial clones for huge repos
Repositories that span gigabytes (operating systems, monorepos) are painful to clone in full. Two complementary features help:
git clone --filter=blob:none --no-checkout <url> repo
cd repo
git sparse-checkout init --cone
git sparse-checkout set src/myteam
git checkout main
--filter=blob:none defers blob downloads until needed, while sparse-checkout populates only the directories you list. Together they let you work on a small slice of a massive repo with proportional disk and bandwidth cost.
Clone-time hooks
Some projects ship setup steps that should run on clone (installing pre-commit hooks, fetching LFS objects, initializing submodules). Git does not run hooks on clone, but you can chain them:
git clone --recurse-submodules <url> repo
cd repo
git lfs install
pre-commit install
For new contributors, document this sequence in CONTRIBUTING.md or wrap it in a scripts/bootstrap script. Smart projects make their make setup target idempotent so re-running it after a stale clone is safe.
Common mistakes
Cloning into a non-empty directory: Git refuses unless you pass an explicit empty target. Cloning huge repositories without --depth on slow connections, then giving up halfway. Cloning over HTTPS to a server that requires SSO, then being unable to push because your token lacks the right scope. Always test with git push --dry-run after the first commit. Finally, cloning into ~/Desktop or another sync folder (Dropbox, iCloud, OneDrive) leads to corruption when the sync client mid-writes .git/index. Keep working clones on plain local storage.