GitClone a Repository (git clone)

Clone a Repository (git clone)

git clone is how you get a copy of an existing Git repository onto your machine. It is the command you run when you join a project, download an open-source library, or set up a fresh laptop from your dotfiles. In one command it copies all history, creates a working directory, and wires up the remote — ready to edit.

The simplest clone

Clone over HTTPS

Bash
git clone https://github.com/torvalds/linux.git

That single command did all of this:

  • Created a new folder called linux in the current directory.

  • Initialised a Git repository inside that folder.

  • Downloaded every commit, branch, and tag from the remote.

  • Created a remote called origin pointing at the URL you cloned from.

  • Checked out the default branch (usually main or master) into the working tree.

Cloning into a specific folder name

Bash
git clone https://github.com/facebook/react.git my-react-fork
# Files appear in ./my-react-fork instead of ./react
HTTPS vs SSH URLs

Most Git hosts offer two URL formats:

Two ways to clone the same repo

Bash
# HTTPS — uses your password or a Personal Access Token
git clone https://github.com/user/project.git

# SSH — uses your SSH key
git clone git@github.com:user/project.git
  • HTTPS works through firewalls and proxies. You authenticate with a token (modern) or password (legacy). Easier to set up.

  • SSH uses public/private key authentication. No passwords needed after initial setup. Preferred by experienced developers.

Tip
Configure SSH keys once and you never type a password again. See the “SSH Keys for GitHub” page for setup.
Cloning a specific branch

Bash
# Clone but check out 'develop' instead of the default
git clone --branch develop https://github.com/user/project.git
# Short flag: -b
git clone -b develop https://github.com/user/project.git
Shallow clones (faster, smaller)

For huge old repositories you may not need the entire history. --depth clones only the latest N commits.

Bash
# Only the latest commit
git clone --depth 1 https://github.com/torvalds/linux.git

# Latest 50 commits
git clone --depth 50 https://github.com/torvalds/linux.git
  • Shallow clones are much faster and use less disk space.

  • They are great for CI pipelines and one-off explorations.

  • You cannot push commits from a shallow clone in the same way — extra steps are needed.

  • You can deepen a shallow clone later with git fetch --deepen N or --unshallow.

Cloning just one branch (with history)

Bash
git clone --single-branch --branch main https://github.com/user/project.git

Useful for CI when you only need one branch and do not want to download every other branch ever pushed.

Including submodules

Bash
# Clone and initialise all submodules in one go
git clone --recurse-submodules https://github.com/user/project.git

# Or, after a normal clone:
cd project
git submodule update --init --recursive
Bare clones (for servers/mirrors)

Bash
# A bare clone has no working tree — used as a "server" copy
git clone --bare https://github.com/user/project.git

# A mirror clone copies every ref exactly — used for backups
git clone --mirror https://github.com/user/project.git
What the result looks like

Inspect what clone gave you

Bash
git clone https://github.com/user/project.git
cd project

git status
# On branch main
# Your branch is up to date with 'origin/main'.

git remote -v
# origin  https://github.com/user/project.git (fetch)
# origin  https://github.com/user/project.git (push)

git branch -a
# * main
#   remotes/origin/HEAD -> origin/main
#   remotes/origin/main
#   remotes/origin/develop

git log --oneline | head -5
Common clone problems
  • fatal: repository not found — wrong URL, or the repo is private and you are not authenticated. Check that you can open the URL in your browser.

  • Permission denied (publickey) — you used the SSH URL but your SSH key is not on GitHub/GitLab. Switch to HTTPS or set up your key.

  • Stuck at “Resolving deltas: 100%” — slow network on a huge repo. Try --depth 1 for a fast partial clone.

  • fatal: destination path already exists — the target folder is not empty. Either delete it, choose a different name, or cd somewhere else.

git clone vs forking
Cloning gives you a local copy you can edit. **Forking** (a GitHub concept) makes a copy of the repo *on GitHub* under your account, which you can then clone. Forks are how you contribute to projects you do not have write access to.
A complete contributor workflow

From zero to your first PR

Bash
# 1. Fork the repo on GitHub (button in the upper-right)

# 2. Clone YOUR fork
git clone git@github.com:you/some-library.git
cd some-library

# 3. Add the upstream as a second remote
git remote add upstream https://github.com/originalauthor/some-library.git

# 4. Create a feature branch
git switch -c fix-typo

# 5. Make changes, commit, push
git commit -am "Fix typo in README"
git push -u origin fix-typo

# 6. Open a Pull Request on GitHub
Tip
For most daily work, plain `git clone <url>` is all you need. The fancy flags are situational — learn them as you encounter the need.