How to setup Github repo while contributing to open source projects .

Why Github is so important?

  1. open source projects have thousands of people contributing to the repo at the same time, this needs a very robust system that is error-proof in creating changes to the code base.

  2. Companies hire people to see that the whole process of editing the code base is bulletproof because one mistake can cost big to the company.

  3. Personally speaking, I find the process of pushing the code and managing branches more difficult than actually solving the bug.

  4. My main aim to write this repo is to give new contributors a pattern in which they can set their local GitHub repo which would not create problems when they are contributing.

Forking Vs Cloning

  1. Forking: Forking is like creating a screenshot of the repo at a particular time. All code a time x will be copied to your branch which generally follows the pattern YourName/nameOfYourorganiztion for example Ninad/cal.com

  2. Cloning: Cloning is like ctrl+c and ctrl+ v to the whole project and it does not create the branch.

  • Ideally, you should fork the repo and then clone that repo to get to your local machine.

Setting up the remote

  1. Remote repo and local repo are two different things.

  2. When we forked the repo we told github to set this repo as remote repo to all the repos set that are cloned from this repo.

  3. Hence we saved a step of setting the remote repo from our local repo.

  4. You can see what is your remote repo by:

git remote -v

Head VS Origin VS Upstream

Upstream: Upstream is the main repo from where you have created the fork.

Origin: Origin nickname to your remote repo. Our forked repo is set as the origin for our local repo

Head: Head is the reference that is pointed towards your last commit.

These terminologies have confused me a lot so it is important to clear them before moving forward.

Keeping up to date with the upstream

  1. There are so many changes happening in the main upstream branch every minute so we need to keep up with those changes in our local repo too.

  2. We make a rule: “Store changes of all upstream in the main branch only”

  3. To take a pull from the upstream we use

git checkout main
git pull upstream main
//This commands will take a pull from the repo from where we forked our repo

Making changes

  1. It is a common pattern to make new branches while creating changes to solve an issue in open source project

  2. Hence we will create a branch from our local repo’s main branch and will do changes to it.

  3. Hence if anything goes wrong we can always have our main branch back in time.

git checkout -b nameOfbranch 
//This command will create new branch and switch to that branch 
git switch branchName
//This command will switch to that particular branch

Committing your changes

  1. Suppose you make to branch “a” and now you want that your changes should be merged to the main repo.

    1. Add all the changes
    git add nameOfThefile
    //To add a particular file for stagging
    git add --all 
    //To add all the files to stagging

b. commit your changes

    git commit -m "Commit Message"
    //This will commit all your  staged changes with a commit message
    //You cannot commit aparticular file so be careful while stagging them for commi
  1. There is a high chance that while you made changes in your local branch the upstream will have had some changes now to copy them to our branch that we created to solve an issue we will be doing the following

    1. Be sure that all your changes are committed before proceeding.

    2. Make sure you get the following message when you run “Git Status”

    On branch <our branch>
    Your branch is up to date with 'origin/<our branch>'.nothing to commit, working tree clean

c. fetch changes and rebase your branch using

     git fetch --all
     git rebase upstream/master
    // This command take the changes from the upstream/main branch and apply them to your current branch.
  1. Now you can finally push changes to the remote by creating a new branch in the remote repo
git push --set-upstream origin gosimple-vpn

Hence, this was whole journey from forking the repo to pushing your changes to remote repo. Don't forget to give your feedbacks from the links provided in my profile. Happy Coding 😊🛫