Open In App

Git – Fork

Improve
Improve
Like Article
Like
Save
Share
Report

Pre-requisite: GitHub

Forking a repository means creating a copy of the repo. When you fork a repo, you create your own copy of the repo on your GitHub account. When several developers want to work on a project but need to make changes that are inappropriate for the original repository, forking is frequently used in open-source software development. Developers can independently make changes by forking the repository, then submit pull requests to the original repository if they want to contribute their changes back upstream.

Definition of Git Fork

By copying the original repository to our personal GitHub account, Git Fork enables us to make changes to the source code without affecting the original repository. where the developer can freely create without concern for the original source code.

Reasons For Forks 

  1. You have your own copy of the project on which you may test your own changes without changing the original project.
  2. This helps the maintainer of the project to better check the changes you made to the project and has the power to either accept, reject or suggest something.
  3. When you clone an Open-Source project, which isn’t yours, you don’t have the right to push code directly into the project. 

Forking A Repository 

Step 1: Open the repository that you want to Fork there You can see the icon as shown in the image below in the repo’s top right corner. Now, this feature is used to Fork the repo. :

Fork

 

Step 2: Go to any repository that you want to Fork here we are using a sample repo of Python official repository. 

Python repository

 

You can see python/cpython. This means python is the maintainer and cpython is the project’s name.  

Step 3: Find the Fork button in the top right corner.

Fork Repo

 

Step 4: Click on Fork

Forking the Repo

 

Step 5: Now you have your own copy of the repository. But how can we confirm for which do refer to below visual aid as follows:

Copy of Repo

 

Now we can see your user name(**********)/CPython and also below that, we have the link to the original project I forked from. 

Whatever changes are made to ‘******/python? We can make my changes here and then make a Pull Request to the maintainers of the project. Now it is in their hand if they will accept or reject your changes to the main project.

How To Fork Using Command Line and How To Install CLI?

 Step 1: Open the terminal or gitbash and type the below command.

gh --version 

If it displays the version like in the image below, then the GitHub ClI was already set up on our computer. If not, use this link to set it up. “https://cli.github.com/”

gh --version

 

Step 2: You must first log in using the CLI to GitHub using your GitHub account before you may fork the repositories. To do it, issue the following command.

gh auth login --web > SSH

Open the CLI-provided link, then enter your one-time code in the Chrome browser. 

gh auth login

 

Step 3: Once authentication is done. Copy the Repo URL that you to fork into our repo and use the below command.

gh repo fork <REPO URL> --clone
gh repo fork

 

— clone is used to get rid of the hurdles in between Fork. That is how we may use the CLI to fork the repository.

Difference between Git Fork vs Git Clone 

Fork

Clone 

To enable independent development without impacting the original repository, a fork is a duplicate of a repository that has been made.

Cloning a repository entails making a local duplicate of an already-existing, remotely hosted Git repository.

When you fork a repository, a new copy of the repository is created under your own account enabling you to experiment and edit as necessary.

A repository is completely duplicated when it is cloned, including all of its files, history, and metadata.

A forking operation produces a fresh, independent copy of the repository that belongs to the forking user and is commonly carried out on a Git hosting service like GitHub or GitLab.

Cloning is commonly carried out via the Git command line on a local system, and it produces an isolated, complete copy of the repository that may be edited and committed locally.

How To Decide Between A Git Clone And Fork?

It is advised to utilize “git clone” if you are employed by an organization as a developer and you wish to make contributions to the source code., which is better suited for the “git push and git pull” operations via which other developers can view and validate the code.

Use “git fork” so other developers can’t access or follow your progress if you wish to construct another project in isolation using existing source code as a base.

Configuring Git To Sync Your Fork with the Upstream Repository 

You must carry out the following actions to set up Git to sync your fork with the upstream repository:

Step 1: Clone your fork repository

Clone the repository that you forked to the directory where you want to store the repository by using the following command.

git clone <Repository URL>

Step 2: Add the upstream remote

Know to Add the upstream repository as a remote to your fork repository. By using the following command we can do this.

cd <forkrepo>
git remote add upstream <Repository URL>

Step 3: Fetch the upstream branches

Use the Git fetch command and fetch the branch required from the upstream repository.

git fetch upstream

Step 4: Add the upstream changes to your local branch

Switch the branch you want to update by using the following command.

git checkout main

Know to merge the branches which are fetched from upstream by using the below command.

Git merge upstream/main

Step 5: Push the changes to your fork repository

 With the help of the following command, we can push the changes to your fork repository.

Git Push

Now that your fork repository and the upstream repository are in sync, you can add changes to your fork as necessary.

Conclusion

In this article we covered the differences between a git fork and a git clone, how to set up git to sync your fork with the upstream repository, and how to fork a repository from GitHub using the GUI and CLI using a sample repository and step-by-step instructions with screenshots.



Last Updated : 08 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads