Git Clone

Updated · 11 min read
easyGit 2.53gitgit-cloneshallow-cloneinterview

When a new student joins a class halfway through the year, they don't start with an empty notebook. They copy a classmate's complete notes, every lesson from the first day, and continue from there.

git clone is that copy for a repository. It downloads the whole project from a server: every file, every commit, every branch, the complete history. You get a full, independent repository on your computer, already connected to the server, ready to work in. It is how almost everyone starts on an existing project, and usually the very first Git command a new team member runs.

In this lesson, you will clone a repository, see what Git set up for you, and use the options for choosing the folder, the branch and how much history to download.

The Starting Point

As in the previous lesson, a bare repository in a folder plays the part of the server, fee-tracker.git. Each person's computer is a folder too: navika and advitha. Navika has already pushed her project, with two commits on main and a branch release/1.0:

Terminal
git init --bare --quiet fee-tracker.git
mkdir navika advitha
cd navika
git init --quiet fee-tracker
cd fee-tracker
echo "Fee Tracker" > README.md
echo 'Term 1 fee: $12,000' > fees.txt
git add .
git commit --quiet -m "Add README and the term fee"
git switch --quiet -c release/1.0
echo "Version 1.0" > VERSION
git add VERSION
git commit --quiet -m "Prepare release 1.0"
git switch --quiet main
echo 'Late fee: $250' >> fees.txt
git commit --quiet -am "Add the late fee"
git remote add origin ../../fee-tracker.git
git push --quiet -u origin main release/1.0
git log --oneline --all
Output
118fe5c (HEAD -> main, origin/main) Add the late fee
582af69 (origin/release/1.0, release/1.0) Prepare release 1.0
c460dae Add README and the term fee

git init --quiet fee-tracker creates the folder and the repository in one step. git push with two branch names pushes both.

Clone a Repository

Advitha joins the project. On her computer, she clones it:

Terminal
cd ../../advitha
git clone ../fee-tracker.git
Output
Cloning into 'fee-tracker'...
done.

Git names the new folder after the last part of the URL, without .git: fee-tracker. With a real server, the command is the same with the URL from the repository page, for example git clone https://github.com/navika-anamaneni/fee-tracker.git. In a terminal you also see progress lines such as Receiving objects: 100%, which Git prints only to a terminal.

Now look inside:

Terminal
cd fee-tracker
ls
git log --oneline
git remote -v
Output
README.md
fees.txt
118fe5c (HEAD -> main, origin/main, origin/HEAD) Add the late fee
c460dae Add README and the term fee
origin	/home/navika/projects/advitha/../fee-tracker.git (fetch)
origin	/home/navika/projects/advitha/../fee-tracker.git (push)

Advitha has the files and the full history, with the same commit IDs as on Navika's computer. For a folder, git clone saves the full path as the URL, which is why git remote -v shows a longer path than the one she typed. A clone is a complete repository, not a download of the latest files.

What Clone Set Up for You

git clone did more than copy:

Terminal
git branch
git branch -a
git status
Output
* main
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/main
  remotes/origin/release/1.0
On branch main
Your branch is up to date with 'origin/main'.
 
nothing to commit, working tree clean

How it works

  1. It added a remote named origin pointing to the URL you cloned from.
  2. It downloaded every branch on the server as a remote-tracking branch: remotes/origin/main and remotes/origin/release/1.0. These are your read-only copies of the server's branches.
  3. It created one local branch, main (the server's default branch), and switched to it.
  4. It linked main to origin/main as its upstream, so git status can say up to date with 'origin/main', and git pull and git push know where to go.

remotes/origin/HEAD -> origin/main records the server's default branch. Notice that there is no local release/1.0 yet. The practice at the end shows how you get one.

Everything git clone sets up

Choose the Folder Name

Add a folder name after the URL to clone into a different folder. A . clones into the current folder, which must be empty.

Start on Another Branch: --branch

--branch (or -b) checks out a branch other than the default one after cloning:

Terminal
cd ..
git clone --branch release/1.0 ../fee-tracker.git fee-tracker-release
Output
Cloning into 'fee-tracker-release'...
done.
Terminal
cd fee-tracker-release
git branch
ls
Output
* release/1.0
README.md
VERSION
fees.txt

The clone still has the whole history and all branches. Only the starting branch is different, so here VERSION is in the folder.

Download Less History: --depth

For a huge project, or a build server that only needs the latest files, a shallow clone downloads only the newest commits. --depth 1 means "only the last commit":

Terminal
cd ..
git clone --depth 1 "file://$(cd .. && pwd)/fee-tracker.git" fee-tracker-shallow
Output
Cloning into 'fee-tracker-shallow'...
Terminal
cd fee-tracker-shallow
git log --oneline
git rev-parse --is-shallow-repository
Output
118fe5c (grafted, HEAD -> main, origin/main, origin/HEAD) Add the late fee
true

Only one commit is there. With a real server, git clone --depth 1 https://... works as it is. A folder on your own disk needs a file:// URL for --depth, because for a plain folder path Git copies the files directly and ignores the depth. That is the only reason for the file:// part here.

A shallow clone is fine for building or reading code, but some commands, such as git log beyond the depth or git blame, can't see the missing history. git fetch --unshallow downloads the rest later.

Syntax

Terminal
git clone [options] <url> [<folder>]
OptionIt is used to
<folder>Clone into this folder instead of one named after the URL
--branch <name> or -b <name>Start on this branch (or tag) instead of the default branch
--depth <n>Download only the newest n commits (a shallow clone)
--single-branchDownload only one branch (automatic with --depth)
--origin <name>Call the remote something other than origin
--recurse-submodulesAlso clone the submodules the project uses

In Practice

On GitHub, GitLab and Bitbucket, the green or blue Code (or Clone) button shows the URL to copy, with tabs for HTTPS and SSH. Two other buttons on the same page are often confused with cloning:

ActionWhat you get
CloneA full repository with history, connected to the server
Download ZIPOnly the files of one commit, with no history and no .git folder
ForkA copy of the repository on the server, under your own account. You then clone your fork. Forks are covered with hosting platforms.

IntelliJ IDEA's "Get from VCS" and VS Code's "Clone Repository" run the same git clone for you.

Common Mistakes

Cloning into a folder that already has the project. Git refuses with destination path ... already exists and is not an empty directory. You only clone once. To get new work later, use git pull.

Cloning inside another repository. The new repository ends up nested inside the old one's working tree, and the outer one sees it as an untracked folder. Clone into a folder outside any repository.

Looking for other branches with git branch. Only main is local after a clone. git branch -a shows the server's branches, and git switch <name> creates a local one.

Downloading the ZIP and expecting git pull to work. A ZIP has no .git folder. Clone instead.

Using --depth 1 for day-to-day development. You can work in a shallow clone, but history-based commands become incomplete. Use a full clone for your own work.

Under the Hood

git clone is a shortcut for commands you already know. It is roughly:

  1. git init <folder> to create an empty repository.
  2. git remote add origin <url> to save the remote.
  3. git fetch origin to download all commits and branches as origin/....
  4. git switch <default-branch> to create the local branch with origin/<default-branch> as its upstream, and fill the working tree.

The downloaded branch names are stored compactly in .git/packed-refs instead of one file each:

Terminal
cd ../fee-tracker
cat .git/packed-refs
Output
# pack-refs with: peeled fully-peeled sorted
118fe5cfa49c2e6ed6e33b1340283969499e9c9a refs/remotes/origin/main
582af6972ac3c225a16d1ccba50508d157e3696f refs/remotes/origin/release/1.0

Each line is a commit ID and a remote-tracking branch name, the same information .git/refs/ holds for branches you create yourself.

Interview Corner

What does git clone do?

It copies a whole repository from a URL: every commit and branch. It creates a remote called origin, stores the server's branches as remote-tracking branches (origin/main), creates and checks out a local branch for the default branch with origin/<branch> as its upstream.

What is the difference between git clone and git pull?

git clone creates a new local repository from a remote, once. git pull updates an existing repository with new commits from its remote, as often as needed.

What is a shallow clone, and when would you use it?

A clone made with --depth <n> that downloads only the newest n commits. It is faster and smaller, which suits CI builds and large repositories, but history-based commands can't see beyond the depth. git fetch --unshallow completes it.

What is the difference between clone and fork?

A clone is a copy of a repository on your computer. A fork is a copy on the hosting server under your own account, used to propose changes to projects you can't push to. You usually fork, then clone your fork.

How do you clone a specific branch?

git clone --branch <name> <url> checks out that branch after cloning, while still downloading all branches. Add --single-branch to download only that branch.

Practice

Challenge: get Navika's release branch. In Advitha's first clone (advitha/fee-tracker), there is no local release/1.0 yet. Run git switch release/1.0, then print the local branches.

What does Git print? Before you open the solution, decide whether git switch fails because the branch doesn't exist locally, or does something else.

Show solution
Terminal
git switch release/1.0
Output
branch 'release/1.0' set up to track 'origin/release/1.0'.
Switched to a new branch 'release/1.0'

It works. Check the local branches:

Terminal
git branch
Output
  main
* release/1.0

There was no local branch with that name, but there was exactly one remote-tracking branch, origin/release/1.0, so git switch created a local branch from it and set it up to track it. This is the everyday way to start working on a branch a teammate pushed.

Checkpoint. Clone a public repository you like from GitHub, GitLab or Bitbucket. Run git branch -a, git remote -v and git log --oneline -5. Then make a shallow clone of the same repository with --depth 1 and compare the size of the two folders.

Quick check: Git clone

4 questions
1What does a clone contain?
2What is the remote called after git clone?
3Right after cloning, which local branches exist?
4What does --depth 1 do?

Key Takeaways

Quick reference

CommandIt is used to
git clone <url>Clone into a folder named after the repository
git clone <url> <folder>Clone into a folder you name
git clone --branch <name> <url>Clone and start on another branch
git clone --depth 1 <url>Download only the newest commit
git branch -aSee local and remote-tracking branches
git switch <branch>Start working on a branch from origin
git fetch --unshallowDownload the rest of a shallow clone's history

Was this tutorial helpful?

Lesson 35 of 106