Git Notes
Forking is GitHub\
Forking is GitHub's mechanism for creating your own copy of someone else's repository. Unlike cloning (which creates a local copy), forking creates a server-side copy on GitHub under your account. This is the foundation of open source collaboration — it lets you freely experiment with changes without affecting the original project, and provides a path to contribute your improvements back via pull requests.
Understanding Forking
When you fork a repository, GitHub creates an exact copy of the entire repository — all branches, all commits, all files — under your GitHub account. This copy is independent: you can push to it freely, create branches, delete files, and make any changes without affecting the original repository.
The fork maintains a connection to the original (called the "upstream" repository), which allows you to:
- Pull in updates from the original when it changes
- Submit pull requests back to the original
- See your fork's relationship to the source project
When to Fork
Contributing to open source: You do not have push access to most open source projects. Forking gives you a copy you can modify, and then you submit a pull request to propose your changes.
Creating your own version: Sometimes you want to take a project in a different direction. Forking gives you a starting point while respecting the original's license.
Experimenting safely: Want to test a risky change to a shared project? Fork it, experiment, and only propose the change if it works.
How to Fork a Repository
On GitHub
- Navigate to the repository you want to fork
- Click the "Fork" button in the upper right
- Select your account (or an organization) as the destination
- GitHub creates the fork in seconds
Using GitHub CLI
gh repo fork owner/repository-name
# This forks AND clones in one step
gh repo fork owner/repository-name --clone=false
# Fork only, don't clone locallyThe Complete Fork Workflow
Step 1: Fork on GitHub
Click the Fork button to create your-username/project-name.
Step 2: Clone Your Fork Locally
git clone https://github.com/your-username/project-name.git
cd project-nameStep 3: Add Upstream Remote
git remote add upstream https://github.com/original-owner/project-name.git
git remote -v
# origin https://github.com/your-username/project-name.git (fetch)
# origin https://github.com/your-username/project-name.git (push)
# upstream https://github.com/original-owner/project-name.git (fetch)
# upstream https://github.com/original-owner/project-name.git (push)Step 4: Create a Branch for Your Changes
git checkout -b feature/improve-docsStep 5: Make Changes and Push to Your Fork
git add .
git commit -m "Improve installation documentation"
git push origin feature/improve-docsStep 6: Create a Pull Request
Go to the original repository on GitHub. You will see a prompt to create a PR from your recently pushed branch.
Keeping Your Fork Updated
The original repository continues to receive updates from other contributors. Keep your fork synchronized:
# Fetch updates from the original repository
git fetch upstream
# Merge upstream changes into your local main
git checkout main
git merge upstream/main
# Push the updates to your fork on GitHub
git push origin mainOr using rebase for a cleaner history:
git checkout main
git pull upstream main --rebase
git push origin mainFork vs Clone
| Aspect | Fork | Clone |
|---|---|---|
| Where | Creates copy on GitHub | Creates copy on your machine |
| Push access | You own the copy | Depends on original permissions |
| Purpose | Contribute without direct access | Work with code locally |
| Relationship | Visible on GitHub (fork network) | Not tracked by GitHub |
| Cost | Free (GitHub feature) | Free (Git feature) |
In practice, you almost always do both: fork first (GitHub copy), then clone your fork (local copy).
Managing Multiple Forks
If you contribute to several projects:
# Organize by keeping upstream remotes clear
cd project-a
git remote -v # Verify upstream is set
cd project-b
git remote -v # Verify upstream is setCommon Mistakes
Forgetting to add upstream remote:
Without the upstream remote, you cannot pull updates from the original. Always add it immediately after cloning your fork.
Making changes directly on main:
Always create a branch for your changes. This keeps your main branch clean and synchronized with upstream, and lets you work on multiple contributions simultaneously.
Fork is out of date when opening a PR:
Always sync your fork before creating a pull request:
git fetch upstream
git rebase upstream/main
git push origin feature/my-change --force-with-leaseKey Takeaways
Forking is the gateway to open source contribution on GitHub. It creates your own copy of a repository where you have full control, while maintaining a connection to the original for synchronization and pull requests. The workflow is: fork on GitHub, clone locally, add upstream remote, create branches for changes, and submit pull requests back to the original. Keep your fork updated regularly to avoid merge conflicts when contributing.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Fork Repository.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, fundamentals, fork, repository, fork repository
Related Git & GitHub Topics