Create Pull Requests in a Flash with Git and Hub
Louis Zawadzki3 min read
At Theodo a lot of our projects follow the Agile git workflow described in another article by my coworker Aurore.
With this worklow, we have to create 2 pull requests every time we finish a feature:
- one for the staging branch that will be merged after the code review
- one for the develop branch that will be merged once the feature has been validated and is ready to be shipped into production
I was spending so much time doing these actions I thought “Hey maybe I can write a git alias to do this for me!”
Introducing hub: the github CLI
Hub is a command-line wrapper built with the go language that enables you to do cool stuff with Github.
For example you can create pull requests, fork repositories or even create a new Github repository straight from your terminal!
Install hub
On Linux
To install hub you first need to install go first.
As for now the latest version of go is 1.7.3, to install it run the following commands:
curl https://storage.googleapis.com/golang/go1.7.3.linux-amd64.tar.gz > go1.7.3.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.7.3.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin
If you want to install go on your system add the last line to your /etc/profile
.
Once go is installed, run the following commands to install hub:
git clone https://github.com/github/hub.git && cd hub
script/build -o ~/bin/hub
If ~/bin/
is not already in your path, add the following line to your ~/.profile
file:
export PATH=$PATH:$HOME/bin
On Mac
To install hub you first need to install go first:
brew install go
If you want to install go on your system add export PATH=$PATH:/usr/local/go/bin
to your /etc/profile
.
Once go is installed, run the following commands to install hub:
brew install hub
Create a git alias to create pull requests
Git aliases are a great way to group your git-related shortcuts.
I personnaly enjoy creating new ones, especially since I know that they can link to any Bash function.
To create an alias that will create your pull requests for staging and develop add this line to your ~/.gitconfig
file in the alias section:
pr = "!f(){ \
hub pull-request -m \"$1\" -b staging -h `git rev-parse --abbrev-ref HEAD` -l \"Please Review\"; \
hub pull-request -m \"$1\" -b develop -h `git rev-parse --abbrev-ref HEAD` -l \"Waiting for validation\"; \
}; f"
What does it do?
First we declare a bash function (it is not really needed here, except for code clarity).
Then we execute two hub commands, which take the following arguments:
-m \"$1\"
sets the first argument passed to the git command as the pull request message-b staging
and-b develop
sets staging or develop as the base branch-h `git rev-parse --abbrev-ref HEAD`
sets the current branch as the head branch-l \"Please Review\"
adds the Please Review label to the pull request
Create pull requests faster than Bruce Almighty
If I am on a branch called feature/a-completely-awesome-feature
and I run this:
git pr "Remove js console error"
It will create both pull requests (on staging and on develop) from my feature branch on my Github remote and that took me less than 5 seconds!
Add templates to your pull requests
My fellow Theodoer William wrote a git extension to handle pull request templates: if such a template is found, your editor will be prompted and prefilled with your template.
You’ll also be able to check the commits in each pull request before opening them!
Here’s the link to the install guide: https://github.com/williamdclt/git-pretty-pull-request/pulls
If you wish to improve this article or if you want to share other cool git aliases, please feel free to comment below!