Cloning_and_Forking
Cloning and Forking
Download repositories and create your own copies.
π₯ git clone
Clone Repository (HTTPS)
git clone https://github.com/user/repo.git
Downloads repository using HTTPS. Prompts for credentials if private.
Clone Repository (SSH)
git clone git@github.com:user/repo.git
Downloads repository using SSH. Requires SSH key setup.
Clone to Specific Folder
git clone https://github.com/user/repo.git my-project
Clones into folder named
my-project.
Shallow Clone
git clone --depth 1 https://github.com/user/repo.git
Downloads only latest commit. Fast for large repos.
Clone Single Branch
git clone --single-branch -b main https://github.com/user/repo.git
Clones only the specified branch.
Clone with Submodules
git clone --recurse-submodules https://github.com/user/repo.git
Clones repository including all submodules.
π Clone Flow
graph LR
A[Remote Repo] -->|git clone| B[Local Copy]
B --> C[Full history]
B --> D[All branches]
B --> E[origin remote set]
π΄ Forking
Fork via GitHub CLI
gh repo fork user/repo
Creates a fork on your GitHub account.
Fork and Clone
gh repo fork user/repo --clone
Forks and clones to local machine.
Fork to Organization
gh repo fork user/repo --org my-org
Forks to an organization instead of personal account.
π Fork vs Clone
graph TD
A[Original Repo] -->|Fork| B[Your GitHub Copy]
B -->|Clone| C[Local Machine]
A -->|Clone only| D[Local - No Push Access]
| Action | Result |
|---|---|
| Clone | Local copy, push to original (if access) |
| Fork | GitHub copy under your account |
| Fork + Clone | Full control, can contribute via PR |
π Working with Fork
Clone Your Fork
git clone git@github.com:YOUR-USERNAME/repo.git
Clone your fork to local machine.
Add Upstream Remote
git remote add upstream https://github.com/ORIGINAL-OWNER/repo.git
Adds original repo as "upstream" remote.
View Remotes
git remote -v
Shows all configured remotes.
Fetch Upstream Changes
git fetch upstream
Downloads changes from original repo.
Merge Upstream Changes
git checkout main
Switch to main.
git merge upstream/main
Merge original repo's changes.
Sync Fork (GitHub CLI)
gh repo sync owner/fork --source upstream/repo
Syncs your fork with upstream.
π‘ Tips
Regularly sync with upstream to avoid large conflicts.
Make changes in feature branch, push to fork, create PR to upstream.
π Related
#git #clone #fork #remote