Photo by vackground.com on Unsplash
How to setup Github repo while contributing to open source projects .
Why Github is so important?
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.
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.
Personally speaking, I find the process of pushing the code and managing branches more difficult than actually solving the bug.
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
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
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
Remote repo and local repo are two different things.
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.
Hence we saved a step of setting the remote repo from our local repo.
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
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.
We make a rule: “Store changes of all upstream in the main branch only”
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
It is a common pattern to make new branches while creating changes to solve an issue in open source project
Hence we will create a branch from our local repo’s main branch and will do changes to it.
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
Suppose you make to branch “a” and now you want that your changes should be merged to the main repo.
- 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
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
Be sure that all your changes are committed before proceeding.
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.
- 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 😊🛫