How to create a new temporary branch in github for a small pull request (for example, bug fix)

This is a summary of my experience with creating a temporary branch on Github for a bug fix. I did not want to create a pull request for all the changes I made in my master branch. I want to create a pull request to upstream with only a bug fix.

First, you can only create a pull request to the upstream with a branch. The easiest approach is to use the Github web UI as explained below

The problem with the pull request is that you can only merge an entire branch to the upstream branch. To do that, I have to create a new branch, let’s call it, “Temp”, and merge “Temp” back to Upstream.

The trick here is that “Temp” includes a small subset of commits from Origin Master branch (my own forked branch).

To do this, we

First, fetch the latest version of upstream

git fetch upstream master

Then, we create a topic branch that tracks the upstream we just fetched

git checkout -b topic track upstream master

Now that we have a topic branch that has the exact same content as the upstream master branch, we apply the commits that involves the bug fixes

git cherrypick commitNumber

When you do cherrypick, it automatically creates a commit for the commits we cherry picked. So there is no need to do additional commits. Once we are done cherry picking, we have topic branch with the commits for the bug fix.

git push origin topic 

Pushes the topic branch to github.

Now we can finally use the github web interface to create a pull request that merges topic branch to upstream master branch. This way, only the bug fixes in topic branch gets merged to the upstream master branch.

This entry was posted in Tools and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s