How to deal with GitHub PRs while working on your feature branch

Adopting the GitHub workflow is very rewarding, specially when working in a team. Code Review is a great practice which GitHub handles very well with its PR (Pull Request) feature.

The not so nice thing is…

While several developers work at the same time on the same git repository, it is very common when someone asks you to review their PR.

That happens to me very often. It used to be a bit painful to have to switch from my feature branch to the PR branch, when I had uncommitted WIP (work in progress).


What I used to do…

Hey, can you review my PR?

So, when someone asked me to review their PR I would usually use git stash to save my work in progress.

git stash
git checkout develop
git pull
git checkout pr-branch

After reviewing and merging PR…

git checkout my-branch
git rebase develop   # to sync my feature branch
git stash show       # To verify if I had stashed my WIP
git stash pop        # To unstash my WIP
What if you git stashed ?

It is very easy to get distracted and forget that you have work stashed in git. Not even oh-my-zshell helps to visualize this. Hence, sometimes I would end up losing my WIP.


What I do now

One day I came across a GitHub on demand training GitHub for Developers from Wheelhouse which back then was free. Even-though, I already had experience working with the GitHub workflow, it was a good idea to go through the training to polish any rough edges.

In this case, the most valuable thing I learned was about how to reset commits. Specially, how to do soft resets.

From then on, I would use a different set of git wizardy when someone asks me,

Hey, can you review my PR?
git commit -m 'WIP whatever' # instead of git stash
git checkout develop 
git pull
git checkout pr-branch

Then, after reviewing and merging in GitHub…

git checkout my-branch
git rebase develop # to sync my feature branch
git log # to see which commit hash was the one from the PR
git reset --soft 2cf4332  # resets from my WIP commits 
# to the PR commit.

This is when the magic happens. You will see all your WIP puffing out of the commits into the git stage as single files. That way you can squash all these code changes into a single compact commit.

You will see something like…

Changes to be committed:
(use “git reset HEAD <file>…” to unstage)
 modified: jsx/app/App.js
modified: jsx/components/Product.js
modified: jsx/components/product/Row.js
modified: jsx/stores/AppStore.js
# finish coding the feature you were working on
# and do the final commit
git commit -m 'Fixes bug #20'

Since I learned about soft resets, I rarely use git stash, nor stress about having to review a PR.

If you have any helpful tips please write a comment below.


One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.