Contributing code and maintenance effort to open source projects is a great way to give back to the software community. Making your pull requests able to be automatically merged saves upstream maintainer’s time and makes the process more efficient. But how can you send a single commit’s worth of code in a pull request?
The easiest way I have found to do this is by using a special
upstream branch and the
git cherry-pick command.
I have contributed to the jStat project, so far mostly in a bug fix / testing capacity. Here’s how I send a PR with a single commit’s worth of code. I have a personal fork of the project for contributions - the fork & merge development model is the preferred method for open source projects on GitHub.
First, I code on whatever feature / bugfix / testing branch I want to. I am sure to keep local changes (for example, I use a slightly different
Makefile than the default) out of commits that contain code for contribution.
When I am ready to submit a PR, I run
git fetch https://github.com/jstat/jstat master. This pulls the master branch of the main project - not my personal fork - and labels it as
Then, I can run
git checkout FETCH_HEAD && git checkout -b upstream. This creates a branch called
upstream that references the main project’s
master branch, as opposed to my personal fork.
Once on the
upstream branch, I can copy over the commit I want with
git cherry-pick <hash>. This applies the single commit to the upstream branch. I run the tests again just to double check, since there could have been changes in the master project since I started working on my contribution.
Finally, I send the commit to the remote GitHub repository with
git push origin upstream --force. Once it is uploaded, I can create a PR with
jstat/jstat:master, the main project, as the base reference, and
jamescgibson/jstat:upstream as the branch to merge, and only the commit I cherry-picked will be included.
This is the easiest way I’ve found to reliably create single-commit, automatically-mergeable pull requests, which should make the project maintainer’s life easier. It also lets me keep my contribution overhead totally separate from any personal changes I make (e.g. editing the makefile for my machine’s peculiarities).