In Git, a fork refers to a personal copy of another user’s repository. When you fork a repository, you create an independent copy that exists within your own account or organization. This copy includes
- All the files
- commit history
- branches present in the original repository at the time of forking.
Why Use Forking?
Forking is helpful in the following scenarios:
- You have your own copy of the project on which you may test your own changes without changing the original project.
- 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.
- When you clone an Open-Source project that isn’t yours, you don’t have the right to push code directly into the project.
Step-by-Step Guide to Fork a Repository
Step 1: Navigate to the Repository
- Open the repository we want to fork on GitHub.
- We will see a Fork button in the top right corner of the repository page.
Git Fork- Click Fork to create a copy of the repository in the GitHub account.
- we can see python/cpython. This means python is the maintainer and cpython is the project's name.
Git RepoStep 3: Confirm the Fork
- After forking, we will see the repository under the account.
- The repository name will appear as the-username/repository-name.
- The original repository link will still be available under the forked repository
- Find the Fork button in the top right corner.
Fork button
Forking a repoStep 4: Make Changes and Create a Pull Request
- After forking, we can make changes in the own repository.
- To contribute to the original project, create a Pull Request (PR).
- The maintainers can review and either accept or reject the changes.
Create a Pull RequestFork Using Command Line
Step 1: Install and Verify GitHub CLI
- Open the terminal or gitbash and type the below command.
gh --version
- If it returns a version number, GitHub CLI is installed. If not, install it from GitHub CLI.
GitBash VersionStep 2: Authenticate GitHub CLI
- Before forking, log in to GitHub using the CLI:
gh auth login --web > SSH
- Follow the authentication steps in the browser.
Authenticate GitHub CLIStep 3: Fork the Repository
- Once authentication is done. Copy the Repo URL that we want to fork into our repo and use the below command.
gh repo fork <REPO URL> --clone
- The --clone flag automatically clones the repository after forking.
Fork the repoDifference between Git Fork vs Git Clone
The table below helps you understand the difference between Git Fork and Git Clone.
Git Fork | Git Clone |
---|
Creates an independent copy of a repository under the account. | Creates a local copy of an existing remote repository. |
we own the forked repository. | we do not own the cloned repository. |
Changes made in a fork do not affect the original repository. | Changes made in a clone stay local unless pushed to a repository. |
Used for local development and testing. | Used for open-source contributions and independent development. |
Configuring Git to Sync the Fork with the Upstream Repository
After forking a repository, we might want to keep it updated with the original project. Follow these steps to sync the fork:
Step 1: Clone the Fork
Clone the repository that we forked to the directory where we want to store the repository by using the following command.
git clone <forked-repository-URL>
Step 2: Add the upstream Repository
Add the upstream repository as a remote to the fork repository. By using the following command we can do this.
cd <forkrepo>
git remote add upstream <original-repository-URL>
Step 3: Fetch the upstream Changes
Use the Git fetch command and fetch the branch required from the upstream repository.
git fetch upstream
Step 4: Merge Upstream Changes into the Branch
Switch the branch we want to update by using the following command. Merge the branches which are fetched from upstream by using the below command.
git checkout main
git merge upstream/main
Step 5: Push the changes to the fork repository
With the help of the following command, we can push the changes to the fork repository.
git push origin main
Now the forked repository is in sync with the original repository.
Explore
Git Tutorial
6 min read
Git Introduction
Git Installation and Setup
All Git Commands
Most Used Git Commands
Git Branch
Git Merge
Git Tools and Integration
Git Remote Repositories
Collaborating with Git