Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Git & GitHub - Forking Repositories

How to fork repositories on GitHub

Forking a repository on GitHub allows you to create a personal copy of someone else's repository. This enables you to experiment with changes, contribute to the original project, and maintain your own version of the repository. This guide explains how to fork repositories on GitHub and manage your fork.

Key Points:

  • Forking creates a personal copy of another user's repository in your GitHub account.
  • You can make changes to the forked repository without affecting the original repository.
  • Contributing back to the original repository is done through pull requests.

Forking a Repository

Step 1: Navigate to the Repository

Go to the GitHub repository you want to fork. For example, https://github.com/octocat/Spoon-Knife.

Fork Button

Step 2: Click the "Fork" Button

Click the "Fork" button in the top-right corner of the repository page. GitHub will create a copy of the repository under your account.

Forking Repository

Step 3: Clone the Forked Repository

Once the repository is forked, clone it to your local machine to start making changes:


# Clone the forked repository
$ git clone https://github.com/your-username/Spoon-Knife.git

# Navigate into the repository directory
$ cd Spoon-Knife
                

Making Changes

Now that you have a local copy of the forked repository, you can make changes, commit them, and push them to your fork on GitHub:


# Create a new branch for your changes
$ git checkout -b my-feature-branch

# Make your changes and stage them
$ git add .

# Commit your changes
$ git commit -m "Add new feature"

# Push the changes to your fork on GitHub
$ git push origin my-feature-branch
                

Keeping Your Fork Up to Date

To keep your fork up to date with the original repository, you need to configure a remote that points to the original repository and pull in the latest changes:


# Add a remote pointing to the original repository
$ git remote add upstream https://github.com/octocat/Spoon-Knife.git

# Fetch the latest changes from the original repository
$ git fetch upstream

# Merge the changes into your local branch
$ git merge upstream/main

# Push the updates to your fork on GitHub
$ git push origin main
                

Contributing Back to the Original Repository

If you want to contribute your changes back to the original repository, you can create a pull request:


# Go to the original repository on GitHub

# Click on the "Pull requests" tab

# Click the "New pull request" button

# Select the branch with your changes and follow the prompts to create the pull request
                

Once the pull request is created, the repository maintainers will review your changes and merge them if they are satisfactory.

Summary

This guide covered how to fork repositories on GitHub, make changes to your fork, keep your fork up to date with the original repository, and contribute back to the original repository using pull requests. Forking is a powerful feature that enables you to collaborate on projects and maintain your own versions of repositories.