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:
- All Files: A complete snapshot of the project's files at a specific point in time
- Complete History: All commit records, author information, and timestamps
- All Branches: Information on all branches in the remote repository
- Remote Association: Automatically creates a connection to a remote repository
(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:
- Participate in open-source projects: Clone a project locally to learn and contribute
- Team Collaboration: Access the team's shared code repository
- Project Deployment: Clone the code to the server for deployment
- Code Backup: Clone the remote repository to your local machine as a backup
2. The git clone command
(1) Basic Syntax
Basic syntax of the git clone command:
# 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:
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
# 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
# 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:
# 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:
- Pros: Fast download speeds, takes up little space
- Disadvantages: Unable to view the full history; certain operations are restricted
- Use Cases: CI/CD builds, when you only care about the latest code
(2) Single-branch cloning
If you only need a specific branch, you can clone just that branch:
# 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:
# 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
# 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:
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:
# 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
# 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:
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
# 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:
# 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
# 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
--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.git push origin <分支名>.git clone <地址> <新目录名>, or enter the existing directory and run git pull to update it.📖 Summary
- Cloning is the process of retrieving a complete copy of a repository from a remote source, including all files, history, and branch information.
git clone <url>is a basic command that allows you to specify the target directory name- Shallow Clone
--depth 1is suitable for scenarios where only the latest code is needed; it is fast and takes up little space. - Single-branch cloning
--single-branchdownloads only the specified branch, reducing the download size - The HTTPS protocol is simple and easy to use, while the SSH protocol offers convenient file transfers; each has its own suitable use cases.
- After cloning, a remote connection to
originis automatically created, allowing you to view remote information and branches. - Cloning only detects the default branch; other branches must be created and tracked manually.
📝 Exercises
-
Basic Exercise: Clone the Hello-World repository on GitHub, view the remote information and all branches, and understand the directory structure after cloning.
-
Advanced Exercise: Compare a full clone and a shallow clone
--depth 1of 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. -
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.



