So you’ve come across a project in GitHub and you’d like to contribute to that project. But how? You don’t consider yourself a developer and source control terminology is foreign to you. No worries. The answer is a Git pull request (PR).
Not a reader? Watch this related video tutorial!In this tutorial, you will learn how to contribute to a GitHub project with a Git pull request to get your code into a GitHub repository (repo).
Contributing to a GitHub Project
By now, you’ve probably seen at least one GitHub project where you feel like you could help fix some bugs, update some documentation or add a new feature. You want to get involved but, like many other IT professionals, you don’t consider yourself a software developer.
Git and GitHub have traditionally been squarely in the development space. But with more and more infrastructure moving to the cloud and Infrastructure as Code tools coming about, sysadmins have been exploring more in GitHub.
Many IT pros don’t know where to start. So, in this article, you’re going to learn from square one how to contribute code to an existing GitHub project with pull requests.
What is a Pull Request?
When you have a code/fix that will add more functionality or give more value to an existing project or repository in GitHub, you can create a pull request.
Even though a pull request is a single action, the end result (getting your code into someone else’s GitHub repo) involves five rough stages you’ll learn in this tutorial.
1. Forking or “copying” the original repo to your GitHub account
The term “forking” is sometimes confused with the term “cloning”. Git and GitHub are two separate products. “Forking” is a GitHub term that creates a replica of a GitHub repo on GitHub. “Cloning” is a Git term that refers to creating a local Git repo on your computer by downloading a remote Git repo.
2. Committing code to our personal forked repo and pushing that code to the GitHub repo
3. Submitting a pull request to the repo owner
4. Repo owner reviewing and approving your changes in the pull request
5. Repo owner merging your changes to the master branch
Prerequisites
To follow along with the demonstrations in this tutorial, be sure you have the following:
- A GitHub account
- A GitHub repo to request changes to – This tutorial will use a repo called git-pull-requests.
- Git – This tutorial will use Git on Windows in a working directory of C:\Git.
- Git set up to work with your GitHub account
Forking the GitHub Repo
Since you cannot make direct changes to someone else’s GitHub repo, you must first create your own. To do that, you need to fork or make a copy of the repo under your own GitHub account. To do that:
1. Open your favorite web browser, navigate to GitHub and log in.
2. Navigate to the tutorial’s repo or the repo you’d like to contribute to as you can see below.
3. In the top-right corner, click on the Fork button. This button will automatically create a copy of that repository in your account.
Once forked, you should now see the same repository in your repository list. You can also see below that this repo was forked from Adam-the-Automator\git-pull-requests.
Committing New Code to Your Personal Fork
Now that you have your own, personal copy of the repo in your GitHub account, it’s time to make your changes. You can make changes to code in a GitHub repo one of two ways; either directly through github.com or locally via Git. Let’s cover both ways.
Committing Code via github.com
The easiest way to make changes to code in a GitHub repo is to simply use the web browser. By using github.com, you don’t have to worry about setting up any kind of software locally. But, using the web browser will soon turn into a headache if you need to perform any moderately complex changes.
Assuming you’re still looking at the forked repo in your browser:
1. Click on the SampleText.ps1 script in your forked repo and click on the pencil icon to edit it.
2. Make change to the file and click on the Commit changes button as shown below optionally adding a commit message.
Committing Code via Git
If you plan on making more than one, simple change to a file in the GitHub repo, you should use Git. Git allows you to clone the entire repo to your local computer and work on the code in your favorite code editor.
Cloning
To work on the forked repo’s code locally, you’ll first need to clone the entire repo to your local computer with Git. To do that:
Assuming you’re still at your forked repo on github.com:
1. Click on the Code button and copy the URL under the HTTPS section.
2. Next, open a command-line console on your computer. This tutorial will use Windows PowerShell.
3. Create a directory to store the cloned repo in and run git clone
pointing it to the URL copied from the previous step.
md C:\Git
cd C:\Git
git clone https://github.com/adbertram/git-pull-requests.git
Committing and Pushing Local Files
Once you’ve cloned the Git repo, it’s time to change the file and commit those changes to the repo. To do that:
1. Open your favorite code editor, edit and save the C:\SampleText.ps1 script.
2. In the terminal window, ensure your working directory is C:\Git and run the below command. The git add
command adds the file to the local Git repo but does not save it. It simply begins tracking it.
cd C:\Git
git add SampleText.ps1
3. Next, commit or save all tracked files in the Git repo with the git commit
command. Be sure to also include an explanation of what has changed (commit message) with the -m
parameter.
git commit -m "modified sampletext file"
4. Now that the local Git repo contains the changed file, push that change up to the GitHub repo with the g
it push
command. You should receive a box asking you to to provide your GitHub username and password to authenticate to your forked GitHub repo.
git push
5. Once you’ve provided your GitHub username and password, you should then see Git provide some status information like below.
Submitting a Pull Request
At this point, you now have a copy of the GitHub repo you’d like to contribute to (forked) in your own GitHub account with the change you’d like to request to the owner GitHub repo. It’s now time to submit a pull request to ask the owner to accept your change.
1. Navigate to your forked repo on GitHub.
2. Click on Pull Requests and the New pull request as shown below.
3. On the pull request page, you’ll see the two repos that will be compared and each commit you’re requesting to merge into the owner’s GitHub repo. Click on Create pull request.
4. Provide a title for the pull request and click on Create pull request.
You will then see on the original GitHub repo, a pull request will show up. At this point, your job is done and it’s time to wait for the owner.
Reviewing and Accepting a Pull Request
As soon as you create a pull request, it will show up under Pull Request under the original GitHub repo as shown below.
At this point, the owner will now either provide comments to you where you can collaborate on the pull request. Or, they can simply merge it right away by clicking on Merge pull request.
Once they merge the pull request with the original code, the pull request will go into a Merged status and the code will be merged with the original repo!
Now, you can see that you are a contributor to the file you changed and was merged.
Conclusion
In this tutorial, you learned each step on how to create a Git pull request. Pull requests are a great way to collaborate on GitHub repos.
Now that you know how to contribute to GitHub projects, what will be your next repo you contribute to?