Cybersecurity Updates & Tools

git clone: Clone a Repository from Remote or Local Sources

git clone copies an existing Git repository into a new directory on your local machine. It initializes a .git directory, downloads all remote branches, sets up tracking branches, and creates a remote called origin pointing back to the source.

How to Use git clone

The basic syntax:

bashgit clone [OPTIONS] REPOSITORY [DIRECTORY]

If DIRECTORY is omitted, Git uses the repository name as the directory name.

Clone over HTTPS:

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

This creates a repo/ directory and checks out the default branch — usually main or master.

Clone over SSH:

bashgit clone git@github.com:user/repo.git

SSH is the better choice for repositories you push to regularly. It uses a key pair for authentication and does not prompt for credentials on each push or pull. Most Git hosting services no longer accept account passwords over HTTPS — if you see “Authentication failed”, use a personal access token instead.

git clone downloads ALL remote branches but checks out only one. Use git branch -r to list all remote branches after cloning.

Clone into a specific directory:

bashgit clone https://github.com/user/repo.git my-project

To clone into the current directory, use . as the target. The directory must be completely empty:

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

Clone a Specific Branch, a Tag, or a Local Repo

Check out a specific branch at clone time with -b:

bashgit clone -b develop https://github.com/user/repo.git

The full repository history is still downloaded — only the checked-out branch differs. To limit the download to only that branch’s history, add --single-branch:

bashgit clone -b develop --single-branch https://github.com/user/repo.git

Clone a specific tag the same way — pass the tag name with -b:

bashgit clone -b v1.0.0 https://github.com/user/repo.git

This checks out in detached HEAD state. Create a branch before making any commits.

Clone a local repository for an isolated copy without affecting the original:

bashgit clone /path/to/local/repo new-copy

Shallow Clones and Common Errors

A shallow clone downloads only a limited number of recent commits instead of the full history. Common in CI/CD pipelines where full history is not needed:

bashgit clone --depth 1 https://github.com/user/repo.git    # latest commit onlygit clone --depth 10 https://github.com/user/repo.git   # last 10 commits

To convert a shallow clone to a full clone later:

bashgit fetch --unshallow

Common errors:

  • Permission denied (publickey) — SSH key is missing, not loaded into the agent, or not added to your account on the hosting service
  • Host key verification failed — SSH cannot confirm the server identity; check ~/.ssh/known_hosts
  • Destination directory is not empty — git clone URL . requires the current directory to be completely empty

Use git clone URL for a standard clone, add --single-branch -b branch to limit what is downloaded, and --depth 1 for CI pipelines. Leave a comment below if you run into any issues.