How to Fork a Repository in Github

Posted by
How to Fork a Repository in Github

Last updated on August 17th, 2020

A fork is a copy of a repository. It allows you to freely experiment with changes without affecting the original project. Here is how to Fork a repo in Github and its workflow. 

Workflow: Forking a GitHub Repository to Submitting a Pull Request

  1. Fork a GitHub repository.
  2. Clone your fork locally.
  3. Add a Git remote for the original repository.
  4. Create a new feature branch to track your changes.
  5. Add, Commit, and Push your changes.
  6. Submit your pull request.
  7. Keep your fork in sync. 

1. Fork a GitHub Repository

  • Log in to your Github account.
  • Find the GitHub repository with which you would like to contribute or work.
  • Press the Fork button on the upper right-hand side of the repository’s page.

2. Clone your new fork locally

Make a local copy of your new forked repository using git clone.

git clone<username>/<forked-repo>.git

3. Add your original repository as Remote

Add your original repository as an upstream remote in your forked repository. It will allow you to pull in changes from your original repository to your forked repository.

cd <forked-repo>
git remote add upstream<username>/<original-repo>.git

4. Create a new feature branch to track your changes

To create a new branch and check it out (meaning tell Git you will be making changes to the branch), use this command:

git checkout -b <new branch name>

5. Add, commit, and push the changes

Add the files you’ve changed and commit them with a descriptive message.

git add .
git commit -m "Add first draft of some feature"
git push -u origin <new branch name>

6. Submit your pull request

You’re now all ready to submit the improvement you’ve made to the project’s maintainers for approval.

1. Go to the original repositories Pull Requests tab.
2. You should see an automatic suggestion from GitHub to create a pull request from your new branch.

7. Keep your fork in sync

Your forked repository doesn’t automatically stay in sync with the original repository.

To keep your fork in sync with the original repository, use these commands:

git pull upstream master
git push origin master