In the last blog, I have shared the information about a general abstract knowledge of what opensource is and what we can do to contribute etc. For those who haven't read that blog can see it here.
Since that blog didn't had a knowledge about how to contribute or how to setup everything, I ll cover all that here in this particular blog. So this blog will cover all the necessary things required to start your very first contribution in open source project.
note: This is a practical blog/tutorial so you can follow along and can get a practical overview about contributing in opensource.
what is git and github?
Git is distributed version control system used by software developers contributing on a single project or software. Basically it helps users to track down the different changes made to the software by different users.
Github is a platform that provides hosting and version control for your code and anyone can colaborate on any project all over the world.
Getting started with Github
The first and the primary step is setting up the environment for which you would be needed to download git in your system.You can download the git for your specific operating system form here.
once you have successfully installed git in your system you can open your command prompt and type the following command to check.
git --version
after this you can go to Github's website and can create an account if you dont have. once you have an account on github with git installed in your pc, You are all set to go.
Your account will look similar to something like this:
So there are certain steps you need to follow to start contributing on github:
- Creating a Repository.
- Forking a Repository.
- Cloning a Repository.
- Creating commits.
- Adding origin/Upstream.
- Pushing code to origin.
- Creating branch.
- Creating issues.
- Creating a pull request.
- Updating your contents of the repo.
Creating a Repository
A repository is a single folder that contains the necessary files required for the project. we can say it as an alternative name for directory. For creating a repo you can see the top right corner of your window where you will have an option to create repository as shown in the fig. below:
once you have clicked you need to fill some information about the repo such as its name, if you want to include Readme file or not or include any files etc. .
Forking a Repository
So whenever you want to contribute in some project of some other person on organization. The first step is to fork the Project that is creating a copy of files of that project for your github account. This needs to be done since you will be changing the files of the project and you don't want that it should affect the original project.
You can locate the button to fork on the top right when you open a particular project as shown in image below:
Cloning a Repository
For setting up environment in your local pc the first need is downloading the files in your pc to run them. this process is known as clonning a repo . you can open your terminal and go to the follwing path where you want to clone files then do the following command:
git clone <url of the repo>
Creating Commits
a commit is basically an event that enables you record the state of the project or save the changes. this is the operation that you performs all the time. there change that needs to be commited must be staged first. the commands of the following are:
git add <filename>
git commit -m "type your message here"
Adding origin and upstream
once you have made the changes in the repo and you want to push it into the github account or any remote account . you will be needed to add origin or upstream for the particular project . since you had cloned your project so the origin of the project will be already set. to view that you can type the following command:
git remote -v
if you dont see anything printed on your terminal then you need to add them. origin is the acronym used for your account from which you have to clone or the place where you need to push the code. upstream is the remote location from which the project have been forked. to add origin and upstream you can do the following commands:
git remote add origin <link of origin repo>
git remote add upstream <link of upstream repo>
Pushing Code To Origin
once you have done everything that was required to change in the local repositry you can update the content of the repo in github by pushing the code to github or uploading it. we can do so by following command:
git push origin main
This will ask you for your userid and password of your github account.Here origin is the location where the files need to be pushed and main is the branch which needs to be pushed.we will know about branch in the later section .
Creating Branch
Since You know that whenever we develop any software or project we do often create new features or fix a bug so we uses branches for that. It is basically a way to maintain the difference in original code with respect to new feature or a bug fix. For checking the existing branch in the project we can type the following command:
git branch -a
To create a new branch we can run the following command in the terminal
git branch <name of new branch>
To checkout or change the current branch you can use the following command
git checkout <name of branch>
It is a good practice to create a new branch for every issue created.
Creating Issues
Once you contribute in any opensource project there is a section in github for issues where you can create an issue. issue basically resembles the particular bug you found in the project or any enhancements for the project or if you need any help from anyone from the community.
In an active project there must be many active issues created by members. if you like to work on any issue you can comment on that issue that you are interested to work on it. The project admin or maintainers will assign the issue to you, Then you can start working on it.
Creating pull Request
For getting the opinion about the contribution that you have made from the collaborators you needed to create a pull Request which describes the changes that you have made.some organizations also have some template or the format that one needs to follow while creating a pull request.
Updating The Contents Of The Repo.
In a system which is open to collaboration for a software from different person we often get the contents changed from the time when we cloned it or forked it so we need to keep on updating the contents of repository. so there are two ways in which you can achieve so:
- using git pull
- using rebase In option 1, You can use the following command:
git pull upstream main
here upstream is the location from where you need to update the content and main is the branch which needs to be pulled.
For option 2, you can use the following command:
git remote update
git rebase origin/main
Rebase is kind of preferred as compared with pull since pull actually is a cobination of git fetch and git merge which actually merge the local changes along with the updates. there can be more explanation on this topic but currently just understand it like this.
Practical Session
Follow the steps in order to create your first pull request.
- Go to the this link and fork the repository
- once You have Forked the Repo clone it into your local pc and keep in mind that you need to copy the url form the forked repo and run the following command
cd Desktop
git clone https://github.com/<your username>/Git_Tutorial.git
- Create an issue at here. you can give any title to your issue.
- Create a branch names "First_PR"
git branch First_PR
- Create a file and write about yourself in that file.The text file should be named as "yourname.txt".windows users can create a text file and save it inside Git_Tutorial.
cd Git_Tutorial
nano yourname.txt
- then create a commit by the following command
git add yourname.txt
git commit -m "file created"
- Push the following code on github
git push origin First_PR
Top comments (0)