Lesson 37 of 60 – Forking a Repository
62%

Forking a Repository

A fork is a personal copy of another user's or organization's repository on a Git hosting platform such as GitHub. Forking allows you to work on a project independently without requiring direct write access to the original repository.

Note: A fork is created on the hosting platform. After creating a fork, you can clone your fork to your computer and work with it using normal Git commands.

1. What is a Fork?

A fork is a copy of a repository under your own GitHub account or organization.

Original Repository
        ↓
      Fork
        ↓
Your GitHub Repository

The fork gives you an independent repository where you can make your own changes.

2. Why Use a Fork?

Forks are commonly used when you want to contribute to a repository without having direct write access to it.

  • Contribute to open-source projects
  • Experiment with another project
  • Make changes independently
  • Prepare pull requests
  • Keep your own copy of a repository

3. Fork vs Clone

Fork Clone
Creates a copy on the Git hosting platform Creates a local copy on your computer
Usually performed through GitHub Performed using the git clone command
Creates a repository under your account Creates a local working repository

4. Forking a GitHub Repository

On GitHub, you can open a repository that you want to contribute to and select the Fork option.

GitHub then creates a copy of the repository under your account or an organization where you have permission to create repositories.

5. Original Repository

The repository from which you create a fork is commonly called the upstream repository in contribution workflows.

Original Repository
       ↓
      Fork
       ↓
Your Repository

The original repository remains separate from your fork.

6. Your Fork

Your fork belongs to your GitHub account or selected organization. You can normally create branches and commits in your fork according to its permissions and settings.

github.com/yourname/project

7. Clone Your Fork

After creating a fork, you can clone your fork to your local computer.

git clone https://github.com/yourname/project.git

Then move into the project directory:

cd project

8. Check the Origin Remote

After cloning your fork, Git normally configures it as the origin remote.

git remote -v

Example:

origin  https://github.com/yourname/project.git (fetch)
origin  https://github.com/yourname/project.git (push)

9. What is Upstream?

In a fork-based workflow, upstream commonly refers to the original repository from which your fork was created.

upstream → Original Repository
origin   → Your Fork

This naming convention helps distinguish your fork from the original project.

10. Adding the Upstream Remote

After cloning your fork, you can add the original repository as an additional remote:

git remote add upstream https://github.com/original-owner/project.git

Now your local repository can communicate with both origin and upstream.

11. Check Multiple Remotes

Use:

git remote -v

You may see:

origin    https://github.com/yourname/project.git (fetch)
origin    https://github.com/yourname/project.git (push)
upstream  https://github.com/original-owner/project.git (fetch)
upstream  https://github.com/original-owner/project.git (push)

12. Fetch Changes from Upstream

To retrieve changes from the original repository:

git fetch upstream

This updates your local remote-tracking information for the upstream repository.

13. View Upstream Branches

After fetching upstream changes, you can view its remote-tracking branches:

git branch -r

Example:

origin/main
upstream/main
upstream/develop

14. Create a Feature Branch

It is common to create a separate branch for your contribution.

git switch -c feature-login

This keeps your feature work separate from the default branch.

15. Make Changes in Your Branch

After creating a feature branch, make the required changes to the project.

git status

git add .

git commit -m "Add login feature"

The commit is created in your local feature branch.

16. Push Your Feature Branch

Push your feature branch to your fork:

git push -u origin feature-login

The branch is then available in your fork on the remote hosting platform.

17. Pull Request from a Fork

After pushing your changes to your fork, you can create a pull request asking the original repository maintainers to review and potentially merge your changes.

Your Fork
    ↓
Feature Branch
    ↓
Pull Request
    ↓
Original Repository

18. Fork-Based Contribution Workflow

Fork repository
      ↓
Clone fork
      ↓
Add upstream remote
      ↓
Create feature branch
      ↓
Make changes
      ↓
Commit changes
      ↓
Push branch to fork
      ↓
Create Pull Request

19. Keeping Your Fork Updated

The original repository may continue to receive changes after you create your fork.

You can retrieve those changes using:

git fetch upstream

You can then integrate the appropriate upstream changes into your local branch according to the project's workflow.

20. Merge Upstream Changes

One way to integrate changes from the original repository is:

git switch main
git fetch upstream
git merge upstream/main

This merges the fetched upstream main branch into your local main branch.

21. Push Updated Main to Your Fork

After integrating upstream changes locally, you can update your fork:

git push origin main

This pushes your local main branch to the origin remote.

22. Fork and Permissions

A fork can allow you to work on a repository even when you do not have direct write permission to the original repository.

However, access to the original repository and its contribution process is controlled by its owners and settings.

23. Fork vs Collaborator Access

Fork Workflow Collaborator Workflow
Work from your own copy Work directly with access to the original repository
Common for external contributors Common for team members with repository access
Usually contributes through pull requests May push branches directly to the repository

24. Fork and Branches

A fork and a branch are different concepts.

  • A fork creates a separate repository copy.
  • A branch creates a separate line of development inside a repository.
  • You can create branches inside your fork.
  • A pull request can compare a branch in your fork with a branch in the original repository.

25. Common Fork Workflow Commands

git clone FORK_URL

cd project

git remote add upstream ORIGINAL_URL

git fetch upstream

git switch -c feature-name

git add .

git commit -m "Add feature"

git push -u origin feature-name

26. Common Fork Problems

  • Wrong fork URL
  • Wrong upstream URL
  • Missing repository permissions
  • Authentication problems
  • Working on the wrong branch
  • Fork is behind the original repository
  • Merge conflicts during synchronization

Use git remote -v to verify your remote configuration.

27. Verify Fork Remotes

git remote -v

A typical fork workflow may show:

origin    https://github.com/yourname/project.git
upstream  https://github.com/original-owner/project.git

Here, origin represents your fork and upstream represents the original repository.

28. Complete Fork Workflow

# Clone your fork
git clone FORK_URL

# Enter project
cd project

# Add original repository
git remote add upstream ORIGINAL_URL

# Get latest upstream information
git fetch upstream

# Create feature branch
git switch -c feature-name

# Make changes
git add .
git commit -m "Add feature"

# Push to your fork
git push -u origin feature-name

29. Fork Checklist

  • Open the repository you want to contribute to.
  • Create a fork under your account or organization.
  • Clone your fork locally.
  • Verify the origin remote.
  • Add the original repository as upstream.
  • Fetch upstream changes when necessary.
  • Create a feature branch.
  • Make and commit your changes.
  • Push the branch to your fork.
  • Create a pull request when ready.

30. Summary of Forking

Forking creates a separate copy of a repository on a Git hosting platform. It is commonly used in open-source contribution workflows.

Original Repository
        ↓
       Fork
        ↓
     Clone
        ↓
 Feature Branch
        ↓
     Changes
        ↓
      Push
        ↓
 Pull Request

In a typical fork workflow, origin points to your fork and upstream points to the original repository.

📌 Key Points

  • A fork is a copy of a repository on a Git hosting platform.
  • Forking is commonly used for contributing to projects without direct write access.
  • A fork and a clone are different concepts.
  • Clone creates a local repository on your computer.
  • After cloning your fork, origin normally points to your fork.
  • upstream commonly refers to the original repository.
  • You can add the original repository using git remote add upstream URL.
  • Use git fetch upstream to retrieve updates from the original repository.
  • Create a feature branch for your changes.
  • Push your feature branch to your fork.
  • Create a pull request to propose your changes to the original repository.

🧠 Quick Quiz

Question: What is a fork in a GitHub workflow?