my-notes

Cloning_and_Forking

1 min read

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

Keep Fork Updated

Regularly sync with upstream to avoid large conflicts.

Contribute Back

Make changes in feature branch, push to fork, create PR to upstream.



#git #clone #fork #remote

Built with mdgarden