Git: Fork Repository to Contribute to Other’s Projects

This article shows how you can for a git repo and push your code contribution by creating a pull request (PR).

Clone vs Fork

Clone

  • Cloning is done directly to the local machine.
  • Updates made to the origin/source repository can be pulled to the local machine (local repo).

Fork

  • Forking is done from origin/source repository to your own GitHub account.
  • After that, you clone the repo from your GitHub account to your local machine.
  • Updates made to the origin/source repository are not reflected in your forked repository.
  • Fork of a public repo is not deleted if the origin/source repo is deleted.
  • Fork of a private repo is deleted if the origin/source repo is deleted.

Fork a repo

For this article, I am using this repo to create a fork: https://github.com/ziadoz/awesome-php

  • To fork the repo, I will open the repo page and click the “Fork” button present in the top right corner of the page.
  • Or, I can also simply add /fork in the repo URL: https://github.com/ziadoz/awesome-php/fork

This will show a page where we can add the forked repo owner, name, and description.

Add the required fields and click on the “Create fork” button.

This will create a forked repo under your account, like this: https://github.com/chapagain/awesome-php

Clone the forked repo

You can use the SSH URL or HTTPS URL to clone.


git clone git@github.com:chapagain/awesome-php.git

OR,

git clone https://github.com/chapagain/awesome-php.git

Add remote upstream (source) repo

List remote branches


git remote -v

origin  https://github.com/chapagain/awesome-php.git (fetch)
origin  https://github.com/chapagain/awesome-php.git (push)

Add remote upstream (source) branch

  • If you have cloned SSH URL of your fork then you have to add the SSH URL of the upstream (source) repo.
  • If you have cloned HTTPS URL of your fork then you have to add the HTTPS URL of the upstream (source) repo.

git remote add upstream git@github.com:ziadoz/awesome-php.git

OR,

git remote add upstream https://github.com/ziadoz/awesome-php.git

List remote repos again


git remote -v

origin  https://github.com/chapagain/awesome-php.git (fetch)
origin  https://github.com/chapagain/awesome-php.git (push)
upstream    https://github.com/ziadoz/awesome-php.git (fetch)
upstream    https://github.com/ziadoz/awesome-php.git (push)

Create new branch

Get the latest updates from the main/master branch upstream


git checkout master
git pull upstream master

Create a new branch for your work/customization


git checkout -b your-branch-name

Add, commit & Push code

List/Check which files are changed


git status

Check diff

Check the difference in the file, i.e. see what changes are made in the file


git diff /path/to/FILENAME

So, if you want to see changes made to README.md then,


git diff README.md

Add file to the index (working tree)


git add README.md

Commit your changes


git commit -m "Your commit message"

Push your changes to your forked repository


git push origin your-branch-name

Create Pull Request (PR)

After you pushed your changes to your forked repository:

  • Open your forked repo page
  • Go to “Pull Request” page
  • Click “New Pull Request” button
  • Select the base repo and branch as your upstream (source) repo and branch.
  • Select the head repo and branch as your forked (origin) repo and branch.
  • Add/Edit the pull request title and description.
  • Click on “Create Pull Request” button.

Hope this helps. Thanks.

Reference: