Cloning in Git

01 Min
🔁
Course Under Active Revision Re-visiting

This article belongs to the Git Basics to Advanced course, which is currently being revisited with updated architectural patterns and content. Content may be refined shortly.

1. Cloning a Remote Repository

Cloning copies an existing Git repository from a remote server (like GitHub, GitLab, or Bitbucket) to your local machine.

When you clone, Git does not merely download the latest files. It replicates the entire commit history, all branches, tags, and internal objects, allowing you to work completely offline and push changes back when ready.

git clone https://github.com/arjit-32/highlight-hero.git

2. Interactive Cloning Visualizer

Observe how cloning sets up your local folder, downloads objects, maps remote tracking branches (origin/main), and checks out your local working branch:

Git Clone Under the Hood

Step 1 of 2: Remote Repository on GitHub
1 / 2
main
master
C1
#c1 Project scaffold
C2
#c2 Add syntax highlighter
HEAD → origin/main
C3
#c3 Export themes package
$ git ls-remote https://github.com/arjit-32/highlight-hero

The remote repository on GitHub contains commit history C1 → C2 → C3 on remote branch origin/main.


3. Cloning Local Repositories

You can also clone repositories already present on your local file system:

# Clone local repo into a new directory
git clone highlight-hero cloned-highlight-hero

4. Useful Cloning Flags

Clone into a Custom Folder

By default, Git creates a directory matching the repository name. You can supply a custom target folder:

git clone https://github.com/arjit-32/highlight-hero.git my-custom-folder

Shallow Clone (--depth 1)

For large repositories with thousands of past commits, fetch only the latest revision to save bandwidth and disk space:

git clone --depth 1 https://github.com/arjit-32/highlight-hero.git

Clone a Specific Branch

To clone only a particular feature or release branch:

git clone -b develop --single-branch https://github.com/arjit-32/highlight-hero.git

5. What Happens During git clone?

  1. Git creates a directory matching the repository name.
  2. Initializes an internal .git database and adds a remote link named origin pointing to the source URL.
  3. Downloads all historical commit objects, trees, and refs.
  4. Sets up remote-tracking branch pointers (remotes/origin/*).
  5. Checks out the default branch (e.g. main or master) so you can begin editing immediately.