Working with Git Forks

Mahesh Chinthaka
2 min readJun 4, 2018

--

working with git forks

If you are a git user you might already know what a git fork is and how it works. As a practice developers do not commit directly to a root repository. Instead they fork the root repo and commit to that forked repository. And then send pull requests to original repo. Those PRs are reviewed and then merged by a person who has merge rights to that repo.

Therefore we have to,

  1. Pull from the root repo to sync with latest updates.
  2. Push to the forked repo with our local commits .
  3. Send pull requests from forked repo to original repo.

We have to configure above after cloning it to our local workspace.

Im going to use following open source repository for my example.

First I will fork the repository for my github profile. Forked repository will be,

Now Im getting source code downloaded to my local machine(clonning).

git clone https://github.com/mcvidanagama/ballerina-lang

Type git remote -v

$ git remote -v
origin https://github.com/mcvidanagama/ballerina-lang.git (fetch)
origin https://github.com/mcvidanagama/ballerina-lang.git (push)

Now you need to set your root repository as your fetching url

You can use git remote set-url command

$ git remote set-url origin https://github.com/ballerina-platform/ballerina-lang.git$ git remote set-url --push origin https://github.com/mcvidanagama/ballerina-lang.git

Type git remote -v again

$ git remote -v
origin https://github.com/ballerina-platform/ballerina-lang.git (fetch)
origin https://github.com/mcvidanagama/ballerina-lang.git (push)

Now you are all set !!!

To update your local repo with latest commits in root repo, type

git pull

To push your local changes to forked repo,

git add <files names to add or * to add all changed files>git commit -m "commit message"git push

To push changes from your forked repo to root repo,

Login to your Github profile and select the repo. Create a pull request to root repo (make sure you select the branch correctly)

Thank you. Happy coding :)

--

--

No responses yet