April 07, 2015

Git Feature Branch Workflow

By Drew Barontini

If you work with Git and GitHub on a large team, then you’re probably familiar with the idea of feature branches.

The core idea behind the Feature Branch Workflow is that all feature development should take place in a dedicated branch instead of the master branch.

Atlassian

Essentially, we want to keep the master branch “green” and always deploy-able to the production environment. This way, we can easily maintain and push new code to master while continuing to build out features on separate branches.

Environment Setup

At Code School, we are constantly iterating to improve our codebase. We utilize feature branches and Pull Requests on GitHub in order to push changes. I’ve made optimizations to my local setup to speed up my feature-branch-to-pull-request workflow.

Bash Function

First things first, let’s set up a Bash function. This function can be added to the ~/.bash_profile (or any equivalent file that’s loading the shell environment settings).

# ----------------------------------------------------
#   Remote Tracking Branch
#   -> Creates a new branch and pushes that branch to remote
# ----------------------------------------------------
#
# $1 - the branch name
#
# Usage: `git_remote_tracking_branch fix_sign_up_form_styles`
#

function git_remote_tracking_branch() {
  git checkout -b $1 && git push -u origin $1
}

It’s a simple function. It creates a new branch and pushes that branch (as a tracking branch) to the remote. So, for example, if there’s a particular GitHub issue to fix, we could, from master, type git_remote_tracking_branch fix_homepage_animation. The function creates the new branch locally and immediately pushes it to the remote.

Tip: The function name should autocomplete, so git_ followed by a tab should autocomplete the function name (assuming there are no other git_ functions). Also, you can always create an alias for something even shorter.

Hub

By itself the above function is useful, but let’s speed up the next part of the workflow: creating a Pull Request on GitHub. To do this, we’ll use Hub, which is a command-line tool from GitHub. You can install it through Homebrew, an excellent package manager for OS X that you should be using, if you aren’t already.

brew install hub

With that installed, we have access to the hub command, which lets us perform a ton of GitHub-specific actions, as well as some more convoluted Git actions, such as:

  • Cloning repositories
  • Creating repositories
  • Adding remote URLs
  • Forking
  • Submitting Pull Requests
  • And a lot more

For our purposes, we’re interested in the hub pull-request command. Once we’ve made our changes on our feature branch and we’re ready to submit the Pull Request, we don’t even have to open GitHub; we can do it all from the command line!

hub pull-request

That command will fire open our editor (or whatever the value of git config core.editor is) to set the title and body of the Pull Request.

To make it even easier, we can set an alias for the hub pull-request command: alias hpr='hub pull-request'.

Flow

Alright, now that we’ve got everything set up, what does the overall flow look like?

$ git_remote_tracking_branch fix_header_style_bug

-> Make changes...

$ hub pull-request (or our 'hpr' alias, if set)

-> Add Pull Request title/body...
-> Save and close the file.

That’s it! We created our feature branch, made our changes, and opened a Pull Request with a nice, quick, efficient workflow.

© 2019 Drew Barontini — Building products under Drewbio, LLC