I love pair-programming with other developers, in part because I always end up learning new ways to improve how I work. One aspect of my workflow that has benefitted from pairing is writing pull requests (or "PRs"). A clear, well-built PR is an essential part of a collaborative workflow, and through pairing, I've picked up several techniques that have made my PRs a little bit better and easier to create.
Contributing to open source? For a great walkthrough on how to create a pull request for an open source project on GitHub, be sure to check out Matt Stauffer's guide.
A well-formed PR should start with a well-named feature branch. At Tighten, we typically use the Git Flow workflow (similar to GitHub Flow). This means we use a main
branch for "production" that is only merged into from develop
. Developers create branches off of develop
for their work on an individual feature, and giving that feature branch a clear name is an important first step when collaborating with others on a single repository.
I picked this tip up from Keith Damiani: if your project has more than two contributors, it can be useful to prefix your branch names with your initials. For example, I prefix lh-
to all my feature branch names: git checkout -b lh-building-a-great-pull-request
. This also allows you to filter down to your personal branches with git branch -v | grep lh-
when there are a lot of active branches in play. It also makes naming branches a bit less daunting, because every branch you create is automatically scoped to you!
For both commit messages and PR titles, it's a good idea to use the present imperative tense — for example, use “Fix dashboard typo” instead of “Fixed” or “Fixes.”
There's a great blog post by Chris Beams entitled "How to Write a Git Commit Message" that's very much worth reading, but one of the most interesting points is his take on the imperative (5. Use the imperative mood in the subject line). One strong reason for using the imperative? Because system-generated commits from Git itself (like "Merge branch...") are written that way.
Also, if you've created a PR that's not quite ready to be merged, give the title a prefix like “WIP:” or “IN PROGRESS:” to avoid someone accidentally reviewing your work prematurely.
If you are a GitHub user, our own Jose Soto made a little bash function in order to streamline the process of opening a new PR:
function gpr() { git push origin HEAD if [ $? -eq 0 ]; then github_url=`git remote -v | awk '/fetch/{print $2}' | sed -Ee 's#(git@|git://)#http://#' -e 's@com:@com/@' -e 's%\.git$%%'`; branch_name=`git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3`; pr_url=$github_url"/compare/main..."$branch_name open $pr_url; else echo 'failed to push commits and open a pull request.'; fi}
With this, typing gpr
in your Terminal will open a fresh "Create Pull Request" screen on GitHub, ready to review!
If you are a hub user, you can create a PR easily, and copy the link to your clipboard, using hub pull-request -m "message" | pbcopy
.
A PR description section is where you let the reviewer know why you opened the pull request; the more information you give them before they look at your actual code, the easier their job will be. A basic description usually has these elements:
I use the alias:
alias desc="git log --no-merges --pretty=format:'- %s' main.. | pbcopy"
to quickly grab a list of commits that haven't yet been merged intodevelop
. The alias copies the commits to my clipboard; from there, I paste the text into the "How?" section and edit it down as needed.
You can save your reviewer a lot of time by allowing them to get the gist (gist--get it?) of your changes without pulling your branch down to their machine and clicking through a UI.
Command-Control-Shift-4
. You can then just paste these screenshots directly into your PR description on GitHub!Capture screenshot
or Capture full-size screenshot
.Here is an example of listing the highlights of your commits at a glance, and giving a preview of the visual aspects of a PR:
develop
branch; resolve any conflicts that might exist, so that merging is as easy as pressing a button.Every developer has their own arsenal of tricks, and we'd love to hear about some of yours! Let us know on Twitter.
We appreciate your interest.
We will get right back to you.