Having trouble figuring out how to contribute code to someone else’s GitHub repo? Git can be quite complicated, so there’s no wonder why you might be having trouble with this. In fact, I found myself today struggling with this same issue attempting to contribute to an open source project for the first time. Thus, here I am writing this post, more or less for my own personal reference. So without further ado… How to Contribute Code to Someone Else’s GitHub Repo
First, you are going to want to fork the other user’s project. This is not intuitive at all. One would think that simply cloning the users project to your local machine and submitting changes from your local project would make the most sense, but this simply isn’t how it’s done. You are going to want to press the “fork” button on the other user’s GitHub.com page to get started. This is your first step.
Next, when you are ready to start working on your new feature or contribution, it’s time to create a “feature branch” which you will be using to develop your new feature or contribution. We’ll start by cloning your recently created fork to your local machine:
$ git clone https://github.com/YourAccount/SuperSweetProject.git
Now let’s create the new branch:
$ git checkout -b my-feature-branch
After creating your new branch, make some changes to the code on your local machine remembering to hit the “save” button in your text editor or IDE. After saving your changes, let’s stage, commit, and push your changes to your forked GitHub repository:
$ git stage .
$ git commit -m "my first open source commit"
$ git push origin my-feature-branch
Now that we’ve pushed our work to the feature branch of our forked GitHub repository, we can push the “submit pull request” button on GitHub.com to contribute our code to the other user’s GitHub repository. Note, we will be contributing the code from my-feature-branch on our forked repository over to the master branch of the other user’s repository. This is the default setting whenever you push the “submit pull request” button from a forked repository on GitHub.com. GitHub assumes you are wishing to submit the pull request to the original repository which you forked.
Assuming that the other user accepts our pull request, now we will need to sync our forked repo once again with the other user’s repo. This is another one of those not-obvious quirks that I found hard to grasp at first. Once we submit a pull request from a feature branch to the other user’s master branch, our master branch on our fork will actually be out of sync with the other users master branch. So this last little gem here will let us get everything back in sync:
(Configure a remote for fork)
$ git remote add upstream https://github.com/OtherUsersAccount/SuperSweetProject.git
(Sync Forked Master Branch with Other User’s Master Branch)
$ git checkout master
$ git merge upstream/master
Now everything should all be in-sync. To contribute more code simply go back to step one, create a new feature branch (with a different name) and repeat the steps above.