Have you ever found a package on GitHub you enjoyed, but wanted to add a quick feature, fix a bug you found, or maybe just contribute to open source as a whole? Join the trend of social coding, share your skills and help benefit the open source world. In this article, learn all about how to contribute code to a repository on GitHub, or any hosted git service.
This article assumes you already have a GitHub account, but if not please quickly register for a free account. Also, ensure you already have git installed by running the following command in terminal:
If instead of seeing the version number you get a “command not found” message, you may install git with the command:
sudo apt-get -y install git
Fork the Repository
First you must choose the repository you wish to contribute to, and visit it in your web browser. If you are unsure of which repository to choose, you may use the demo repository located at https://github.com/mdizak/muo_demo. This is a simple demo, and you are welcome to create pull requests against it for testing and learning purposes.
While viewing the repository in your web browser, look for the Fork Repository button, and click it. This will fork the repository to your own GitHub account, and bring you to it. Next you need to clone the newly forked repository, so open terminal on your local computer and run:
git clone https://github.com/myusername/muo_demo.git
Naturally, ensure you change the above URL to that of your newly forked repository. Once cloned, add the original repository as an upstream with the command:
git remote add upstream https://github.com/mdizak/muo_demo.git
Ensure to change the URL in the above command with that of the original repository you cloned. This signifies to git that its a parent repository from which you are working on.
Prepare Local Repository
Every time before you begin working on a new contribution, ensure you are running the latest code base of the parent repository. Within the project directory, run the following commands in terminal:
git checkout master
git pull upstream master && git push origin master
The first command ensures you’re currently working in the master branch, and the second command syncs the parent repository with both your local and GitHub repositories. This ensures you are modifying the most updated code base, and are not working with outdated code.
Also make sure to check if the project has a contribute.md file, and if so, please ensure to follow what it says. Sometimes projects have specific guidelines regarding how to provide contributions, which should always be followed.
Create a New Branch
You need to create a new branch that will hold all modifications for this contribution, which will later be merged into the main GitHub repository by the project maintainers. The branch must be alpha-numeric, and can be named anything you wish.
Although not required, it’s generally considered common courtesy to prefix the branch name with hotfix/ for quick bug fixes or feature/ for added features. Choose your desired branch name, and run the command:
git checkout -b feature/my_cool_feature
You should always create a separate branch for each feature / bug fix, and never include multiple contributions in one. This helps maintain a smooth development flow, and also helps ensure your contribution is accepted and included in the project.
Now that your branch has been created, go ahead and complete any necessary modifications you would like with the standard git work flow.
Commit and Create Pull Request
Once you have completed work on your contribution, commit it with a proper commit message and push it to your forked repository with the commands:
git commit --file commit.txt
git push -u origin feature/my_cool_feature
The last step is to create a pull request alerting the project maintainers a new contribution is pending. Visit the forked GitHub repository in your web browser, and you will see a dropdown button allowing you to change branches. Change to your newly created branch, and the next page will contain a link allowing you to create a new pull request.
Upon clicking that link you will see a page that shows your commit message, and all changes you have made within the code. Review everything to ensure it is correct, add any necessary additional message for the maintainers, and submit the pull request.
Continue Contributing to GitHub
Congratulations, you’ve successfully submitted a contribution to a GitHub project!
Your pull request is now awaiting review and approval by the project maintainers, and you will be notified via e-mail whether or not it was approved, and with any messages they have for you. Now that you’ve learned how to contribute to projects on GitHub, keep moving forward, and help grow the open source community.