Cloning
20.1 What Cloning Does
When you clone a repository, Git:
• Downloads the entire commit history
• Creates a local working directory
• Automatically sets up the origin remote
• Links local branches to remote-tracking branches
Cloning is one-time setup per machine.
• Downloads the entire commit history
• Creates a local working directory
• Automatically sets up the origin remote
• Links local branches to remote-tracking branches
Cloning is one-time setup per machine.
20.2 Basic Clone Command
1git clone <repository-url>Example:
1git clone https://github.com/username/project.git20.3 Cloning with SSH vs HTTPS
HTTPS
• Easy to start
• Requires authentication on push
SSH
• Requires SSH key setup
• No repeated authentication
• Preferred for long-term work
1git clone https://github.com/username/project.git• Requires authentication on push
SSH
1git clone git@github.com:username/project.git• No repeated authentication
• Preferred for long-term work
20.4 Cloning into a Custom Directory
1git clone <repo-url> <folder-name>Example:
1git clone https://github.com/username/project.git my-project20.5 Shallow Cloning (Partial History)
To download only recent commits:
Useful when:
• Repo is very large
• You only need the latest state
• History is not required
1git clone --depth 1 <repo-url>Useful when:
• Repo is very large
• You only need the latest state
• History is not required
20.6 Cloning a Specific Branch
1git clone -b develop <repo-url>This checks out the specified branch instead of the default one.
20.7 Fork vs Clone (Quick Clarification)
• Clone → local copy of a repository
• Fork → server-side copy under your GitHub account
Typical open-source flow:
1. Fork repository
2. Clone your fork
3. Push changes to your fork
4. Create a Pull Request
• Fork → server-side copy under your GitHub account
Typical open-source flow:
1. Fork repository
2. Clone your fork
3. Push changes to your fork
4. Create a Pull Request
20.8 Key Takeaways
• git clone creates a full local copy of a remote repository.
• Cloning automatically sets up origin and branch tracking.
• SSH cloning is preferred for frequent contributors.
• Shallow clones save time and space for large repositories.
• Forking and cloning serve different purposes.
• Cloning automatically sets up origin and branch tracking.
• SSH cloning is preferred for frequent contributors.
• Shallow clones save time and space for large repositories.
• Forking and cloning serve different purposes.