Trusted answers to developer questions

How to work with Git forks

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

Forking the repository is one way to collaborate on projects in Github. This is especially useful when you do not have write access to the original repository. It allows you to contribute to the repository along with allowing the owner(s) to have control over who can write to the repository.

When working with open source projects, Forking the repository and working with it is the only solution in these situations.

Method

Go to the repository you would like to fork. On Github, it looks like the picture shown below. Forking a repository creates a copy of the repository and allows you to make changes to it without affecting the upstream repository.

widget

Forking a repository will create a copy of the repository into your account. Now you can use this copy to work. The challenge lies in keeping it up to date with the upstream repository (original repository)

I will cover the steps that enable you to do this below.

Step 1

Clone the forked repository to your local machine.

# your_fork_url is the url of the repository copy in your account.
git clone <your_fork_url>

Step 2

Set your upstream to be able to pull down updates from the master repository.

# upstream_git_repo_url is the url of the original repo on github.
git remote add upstream <upstream_git_repo_url>

Step 3

Verify the upstream was added. You can use -v to verify if the upstream was added.

git remote -v
# This lists the urls as shown below
origin git@github.com:anjanashankar9/repo.git (fetch)
origin git@github.com:anjanashankar9/repo.git (push)
upstream git@github.com:original/repo.git (fetch)
upstream git@github.com:original/repo.git (push)

Step 4

Lastly, update the git repo from the master by the following commands:

git fetch upstream
git merge upstream/master

Forking vs Cloning

Forking is something you do on the Github Account while Cloning is a git operation. Irrespective of whether you fork the repository or not, you need to clone the repository in order to effectively work on it.

A more detailed explanation can be found here in this edpresso shot by Arash

RELATED TAGS

git
fork
Did you find this helpful?