GIT Pull Requests Using GitHub
Join the DZone community and get the full member experience.Join For Free
we’ve been working with git for more than a year.
the scm was migrated from svn, with all its history.
our habits were migrated as well.
our flow is (was) fairly simple:
master branch is were we deploy our code from.
when working on a feature, we create a feature branch. several people can work on this branch.
some create private local branch. some don’t.
code review is done one-on-one. one member asks another to join and walks through the code.
introducing pull request to the team
recently i introduced to the team, with the help of a teammate the
it takes some time to grasp the methodology and see the benefits.
however, i already start seeing improvements in collaboration, code quality and coding behaviors.
when a person does a change and calls for a pull request, the entire team can see the change.
everyone can comment and give remarks.
discuss changes before they are merged to the main branch.
everyone knows about the change and anyone can check and comment. the result is that each one can “own” the code.
it helps each team member to participate in coding and reviewing any piece of code.
there’s extra revision of the code before it is merged.
branches can be (imho should be) deleted after merging the feature.
git history (the log) is clearer. (this one is totally dependent on the quality of comments)
improved code quality
i see that it improves the code quality even before the code review.
people don’t want to introduce bad code when knowing that everyone can watch it.
better code review
we’ve been doing extensive code review since the beginning of the project.
however, as i explained above, we did it one-on-one, which usually the writer explained the code to the reviewer.
in my perspective, by doing that, we miss the advantages of code review. the quality of the code review is decreased when the writer explains the material to the reviewer.
using pull request, if the reviewer does not understand something, it means that perhaps the code is not clean enough.
so more remarks and comments, thus, better code.
when a senior does code review to a junior, one-on-one, nobody else sees it.
it’s more difficult for the senior to show case the expectations of how the code should look like and how code review should be performed.
(there are of course other ways passing it, like code dojos. and, pair-programming, although it’s also one-on-one).
by commenting review in the pull request, the team can see what’s important and how to review.
everyone benefits from review of other team members.
improved git usage habits
when someone collaborates with the whole team, he/she will probably write better git comments.
the commits will be smaller and more frequent, as no one wants to read huge amount of diff rows. so no one wants to “upset” the team.
using pull requests forces the usage of branches, which improves git history.
others may call this section as
but the way i see it, it’s more of complaints of “why do we need this? we’re good with how things were till now”
i get too many email already
well, this is true. using pull request, we start getting much more emails, which is annoying.
there’s too much noise. i might not notice important emails.
the answer for that is simple:
if you are part of this feature, then this mail is important because it mentions code changes in some parts that you are working on.
if you want to stop receiving emails for this particular pull request, you can ask to mute it.
if we start emailing, we’ll stop talking to each other
i disagree with this statement.
it will probably reduce the one-on-one review talks. but in my (short) experience, it improved our verbal discussions.
the verbal discussion come after the reviewer watched the code change. if a reviewer did not understand something, only then she will approach the developer.
the one-on-one discussions are much more efficient and ‘to the point’.
ahh ! i need to think on better commit comments. now i have more to think of
this is good, isn’t it?
by using pull requests, each one of the team members need to improve the way comments are written in the commits.
it will also improve git habits. in terms of smaller commits in shorter time.
it’s harder to understand. i prefer that the other developer will explain to me the intentions
don’t we miss important advantages of code review by getting a walk though from the writer?
i mean, if i need to have explanation of what the code does, then we better fix that code.
so, if it’s hard to understand, i can write my comments until it improves.
in this section i will explain briefly the way we chose to use pull requests.
the screenshots are taken fron github, although bitbucket supports it as well.
branching from the “main” branch
i did not write ‘master’ intentionally.
let’s say that i work on some feature in a branch called feature_a (for me, this is the main branch).
this branch was created from master.
let’s say that i need to implement some kind of sub feature in feature_a.
example (extremely simple): add tostring to class person.
then i will create a branch (locally out of feature_a):
# on branch feature_a, after pull from remote do: # git checkout -b <branch-name-with-good-description> git checkout -b feature_a_add_tostring_person # in order to push it to remote (github), run this: # git push -u origin <branch-name-with-good-description> git push -u origin feature_a_add_tostring_person # pushing the branch can be later
doing a pull request
after some work on the branch, and pushing it to github, i can ask for pull request.
there are a few ways doing it.
the one i find “coolest” is using a button/link in github for calling pull request.
when entering github’s repository in the web, it shows a clickable notation for the last branch that i pushed to.
after sending the pull request, all team members will receive an email.
you can also assign a specific person to that pull request if you want him/her do the actual code review.
changing the branch for the diff
by default github will ask to do pull request against
as explained above, sometimes (usually?) we’ll want to diff/merge against some feature branch and not master.
in the pull request dialog, you can select to which branch you want to compare your working branch.
code review and discussion
any pushed code will be added to the pull request.
any team member can add comment. you can add at the bottom of the discussion.
and, a really nice option, add comment on specific line of code.
merging and deleting the branch
after the discussion and more push code, everyone is satisfied and the code can be merged.
github will tell you whether your working branch can be merged to the main (diff) branch for that pull request.
sometimes the branches can’t be automatically merged.
in that case, we’ll do a merge locally, fix conflicts (if any) and then push again.
we try to remember doing it often, so usually github will tell us that the branches can be automatically merged.
after the pull request is merged, it is automatically closed.
if you are finished, you can delete the branch.
- who should merge?
- who should delete the branch?
we found out that it most sensible that the person who initiated the pull request would merge and delete.
the merge will be only after the reviewer gave the ok.
helpful git commands
here’s a list of helpful git commands we use.
# automatically merge from one branch (from remote) to another # on branch branch_a and i want to merge any pushed change from branch_b git pull origin branch_b # show branches remotly git remote show origin # verify which local branch, which is set to upstream can be deleted git remote prune origin --dry-run # actual remove all tangled branches git remote prune origin # delete the local branch git branch -d <branch-name>
Opinions expressed by DZone contributors are their own.