Setting git for contributing

Posted on 2019-10-08
Tags: git opensource

Problem

Recently, I have been trying to contribute more to open source and wanted to have a similar git workflows to my personal and professional projects.

To contribute to a Github Project, you would have to fork the repo, create and push branch with your changes, submit the PR. A few days later, if you want to make a new contribution you would then create a new branch again.

In a fork, fetching doesn't pull from the the original repo. It pulls from your forked version of the repo which doesn't update or change. This becomes a mental burden to remember to pull from the original repo and merge to your master.

Solution

Setting Up

Fork Repo

Go to github and fork the the desired repository. This creates a clone of the repo under your user.

Clone the forked repos

Clone the repo locally

git clone [email protected]/username/example

Set upstream remote

Create a new remote pointing to the original repository. For the code snippet below, we named the original remote to be named upstream.

git remote add upstream https://github.com/source/example
git fetch upstream

Set master of local to track upstream

Edit you .git/config so that the master branch follows the remote upstream. You can also change the source branch if the original repo uses a dev branch.:

[branch "master"]
	remote = upstream # This was origin
	merge = refs/heads/master # dev branch of original repo

or run the following command

git branch master --set-upstream-to=upstream/master

"New" workflow

The end workflow would be identical to my current workflow for my other projects.

  1. Checkout master
  2. Pull from origin
  3. Branch out

or the equivalent git commands would be

git checkout master
git pull
git checkout -B example-branch

Alternatives

Alternatively you can set up the remote, branch of the remote, and just pull from remote.

The workflow for that would be

git checkout upstream/master
git pull
git checkout -B example-branch