Getting Started with Cloning Git Repositories and Remote Collaboration

Cloning is the process of retrieving a complete copy of a Git repository from a remote server, including all files, branches, and the full commit history. This is the first step in collaborating with a team.

1. Overview of Cloning

(1) What is cloning?

Cloning is the process of copying a remote repository in its entirety to your local machine. Unlike downloading a ZIP archive, cloning retrieves the complete Git repository, including:

(2) The Difference Between Cloning and Downloading

Many people ask: Why not just download the ZIP file? Let's compare the two:

Action Contents Can history be viewed? Can changes be pushed? Can the version be switched?
git clone Full repository
Download ZIP File snapshot only

(3) Use Cases for Cloning

Cloning is primarily used in the following scenarios:


2. The git clone command

(1) Basic Syntax

Basic syntax of the git clone command:

BASH
# Clone to the current directory(Automatically create a folder with the same name)
git clone <Warehouse Address>

# Clone to the specified directory
git clone <Warehouse Address> <Directory Name>

(2) Cloning Process

The complete cloning process is as follows:

100%
sequenceDiagram
    participant L as Local Warehouse
    participant R as Remote Server
    
    L->>R: 1. Initiate a Cloning Request
    R->>L: 2. Send Repository Metadata
    R->>L: 3. Send All Objects (Documents, Commits)
    R->>L: 4. Send Branch Information
    L->>L: 5. Create origin/Remote Association
    L->>L: 6. Check out the default branch
    L->>L: 7. Cloning Complete

▶ Example: Cloning a GitHub Repository

BASH
# Clone React Project (HTTPS Method)
git clone https://github.com/facebook/react.git

# Output Information:
# Cloning into 'react'...
# remote: Enumerating objects: 156789, done.
# remote: Counting objects: 100% (156789/156789), done.
# remote: Compressing objects: 100% (456/456), done.
# remote: Total 156789 (delta 12345), reused 156333 (delta 12000), pack-reused 0
# Receiving objects: 100% (156789/156789), 45.67 MiB | 5.23 MiB/s, done.
# Resolving deltas: 100% (123456/123456), done.

# Clone to the specified directory
git clone https://github.com/facebook/react.git my-react-project

# Clone to the current directory(Pay attention to the last point)
git clone https://github.com/facebook/react.git .

▶ Example: Viewing Information After Cloning

BASH
# Clone the repository
git clone https://github.com/user/demo.git
cd demo

# View Remote Repository Associations
git remote -v
# origin  https://github.com/user/demo.git (fetch)
# origin  https://github.com/user/demo.git (push)

# View All Branches(Including remote branches)
git branch -a
# * main
#   remotes/origin/HEAD -> origin/main
#   remotes/origin/main
#   remotes/origin/develop
#   remotes/origin/feature

# View Commit History
git log --oneline -5
# a1b2c3d (HEAD -> main, origin/main, origin/HEAD) Update README
# e4f5g6h Add new feature
# i7j8k9l Fix bug in login
# m0n1o2p Initial commit

3. Cloning Options

(1) Shallow Cloning

For large projects, a full clone can take a long time and require a lot of space. A shallow clone only downloads the most recent commits:

BASH
# Clone only the most recent commit
git clone --depth 1 <Warehouse Address>

# Clone RecentNNext Submission
git clone --depth 10 <Warehouse Address>

Characteristics of shallow cloning:

(2) Single-branch cloning

If you only need a specific branch, you can clone just that branch:

BASH
# Clone only the specified branch
git clone --branch main --single-branch <Warehouse Address>

# Abbreviated form
git clone -b main --single-branch <Warehouse Address>

(3) Recursive Cloning

If a project contains submodules, you need to clone them recursively:

BASH
# Recursive Cloning,Clone all submodules at the same time
git clone --recursive <Warehouse Address>

# If you have already cloned it,Submodules can be initialized individually
git clone <Warehouse Address>
cd <Table of Contents>
git submodule update --init --recursive

(4) Comparison of Cloning Options

Option Description Downloads Use Cases
No options Full clone All Daily development
--depth 1 Shallow clone Latest commit only CI/CD, deployment
--single-branch Single branch Specify a branch only Only a specific branch
--recursive Recursive Cloning Includes Submodules Project Has Submodules
--bare Bare-metal server No workspace Server image

▶ Example: Shallow Cloning of a Large Project

BASH
# LinuxFull Kernel Cloning(3GB+,It takes a long time)
git clone https://github.com/torvalds/linux.git
# Cloning into 'linux'...
# Receiving objects: 100% (8234567/8234567), 3.45 GiB | 1.23 MiB/s, done.

# Shallow CloningLinuxKernel(100MB+,Fast)
git clone --depth 1 https://github.com/torvalds/linux.git
# Cloning into 'linux'...
# Receiving objects: 100% (12345/12345), 156.78 MiB | 5.67 MiB/s, done.

# View Historical Differences
cd linux
git log --oneline
# Only1Next Submission(Shallow Cloning)

# Fully cloned repository
git log --oneline | wc -l
# 1M+ Next Submission

4. Remote Address

(1) Protocol Type

Git supports multiple protocols for accessing remote repositories:

100%
graph TB
    A[Remote Repository Address] --> B[HTTPS]
    A --> C[SSH]
    A --> D[Git Protocol]
    A --> E[Local Path]
    
    B --> B1[Example: https://...]
    C --> C1[Example: git@...]
    D --> D1[Example: git://...]
    E --> E1[Example: /path/to/...]
    
    style B fill:#d4edda
    style C fill:#c3e6cb
    style D fill:#fff3cd
    style E fill:#f8d7da

(2) HTTPS vs. SSH

The two most commonly used protocols are HTTPS and SSH:

Feature HTTPS SSH
Address format https://github.com/user/repo.git git@github.com:user/repo.git
Configuration Difficulty Easy, no configuration required Requires generating and configuring SSH keys
Authentication Method Username + Password/Token SSH Key
Push Action Credentials required each time No password required
Firewall Friendly, uses port 443 May be blocked, uses port 22
Recommended Scenarios Beginners, occasional use Daily development, automation

(3) Using a Personal Access Token

Starting in 2021, GitHub no longer supports password-based authentication; you must use a Personal Access Token:

BASH
# Use when cloningToken
git clone https://<token>@github.com/user/repo.git

# Or enter it when sending the push notificationTokenAs a password
git clone https://github.com/user/repo.git
# Username: your-username
# Password: ghp_xxxxxxxxxxxx(UsageToken)

▶ Example: Cloning Using Different Protocols

BASH
# HTTPSMethod(Recommended for Beginners)
git clone https://github.com/facebook/react.git

# SSHMethod(Recommended for Experienced Developers)
git clone git@github.com:facebook/react.git

# GitAgreement(Read-only,Used less frequently)
git clone git://github.com/facebook/react.git

# Local Path(Clone the local repository)
git clone /path/to/local/repo.git

# Clone from another user's directory on the same server
git clone file:///home/otheruser/project.git

5. Post-Cloning Configuration

(1) Automatically Created Associations

Once the clone is complete, Git will automatically create the following configuration:

100%
graph TB
    A[Cloned repository] --> B[.git Directory]
    A --> C[Workspace Files]
    A --> D[Remote origin Association]
    
    B --> B1[All Commit History]
    B --> B2[All Branch Information]
    B --> B3[Profile]
    
    D --> D1[fetch Address]
    D --> D2[push Address]
    D --> D3[Default Branch Tracking]
    
    style A fill:#e1f5ff
    style D fill:#d4edda

(2) View Remote Information

BASH
# View the name of the remote repository
git remote
# origin

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

# View details about a remote repository
git remote show origin
# * remote origin
#   Fetch URL: https://github.com/user/repo.git
#   Push  URL: https://github.com/user/repo.git
#   HEAD branch: main
#   Remote branches:
#     main     tracked
#     develop  tracked
#   Local branch configured for 'git pull':
#     main merges with remote main
#   Local ref configured for 'git push':
#     main pushes to main (up to date)

(3) Cloned branch

Cloning retrieves all remote branches, but only checks out the default branch:

BASH
# View local branches
git branch
# * main  (Only the default branch)

# View All Branches(Including remote)
git branch -a
# * main
#   remotes/origin/HEAD -> origin/main
#   remotes/origin/main
#   remotes/origin/develop
#   remotes/origin/feature-login

# Create a local branch to track a remote branch
git checkout -b develop origin/develop
# Or use the abbreviation
git checkout develop
# Branch 'develop' set up to track remote branch 'develop' from 'origin'.
# Switched to a new branch 'develop'

▶ Example: Complete Workflow After Cloning

BASH
# 1. Clone the repository
git clone https://github.com/user/project.git
cd project

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

# 3. View Remote Information
git remote -v

# 4. View All Branches
git branch -a

# 5. Switch to another branch
git checkout develop

# 6. View Commit History
git log --oneline --graph --all -10

# 7. Get the latest updates
git pull origin main

❓ FAQ

Q What is the fundamental difference between cloning and downloading a ZIP file?
A Cloning creates a complete Git repository that includes the entire history, branch information, and version control metadata. You can view the history, switch between versions, create branches, and push changes. Downloading a ZIP file simply provides a snapshot of the files; it does not include version control functionality, so you cannot perform any Git operations.
Q What can I do if cloning a large repository is slow?
A You can use a shallow clone --depth 1 to download only the latest commits, which will be much faster. If you only need a specific branch, use the --single-branch option. Additionally, you can use a domestic mirror site to speed up the process, such as GitHub's fastgit mirror.
Q How do I push to a remote repository after cloning?
A You need push permissions. Public repositories can only be cloned, not pushed to. For repositories with permissions, you’ll need to enter your username and token when using HTTPS, or configure an SSH key when using SSH. The push command is git push origin <分支名>.
Q How do I clone a private repository?
A Private repositories require authentication. For HTTPS, use your username and Personal Access Token; for SSH, use a configured SSH key. Make sure your account has access permissions; otherwise, you’ll see an “Authentication failed” message.
Q What if I get "fatal: destination path already exists"?
A The target directory already exists. You can delete the existing directory and clone it again, clone it to a different directory git clone <地址> <新目录名>, or enter the existing directory and run git pull to update it.

📖 Summary


📝 Exercises

  1. Basic Exercise: Clone the Hello-World repository on GitHub, view the remote information and all branches, and understand the directory structure after cloning.

  2. Advanced Exercise: Compare a full clone and a shallow clone --depth 1 of a large project (such as React), compare the download time and the size of the .git directory, and understand the use cases for shallow clones.

  3. Challenge: Research how to clone a repository locally and then push it to another remote repository (e.g., cloning from GitHub and pushing to GitLab), and gain an understanding of remote repository management and multi-remote configurations.

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏