Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A shell command to push and open a pull request. Tested on OSX/Ubuntu. No dependencies.
#!/usr/bin/env bash
# gpr - push the current branch to origin and attempt to open the Create Pull
# Request or Create Merge Request page if available.
# Inspired by:
# How to get the current branch:
# How to open the browser:
# Colour constants for nicer output.
# Push the current branch to origin, set upstream, open the PR page if possible.
gpr() {
# Get the current branch name, or use 'HEAD' if we cannot get it.
branch=$(git symbolic-ref -q HEAD)
# Pushing take a little while, so let the user know we're working.
echo "Opening pull request for ${GREEN}${branch}${RESET}..."
# Push to origin, grabbing the output but then echoing it back.
push_output=`git push origin -u ${branch} 2>&1`
echo ""
echo ${push_output}
# If there's anything which starts with http, it's a good guess it'll be a
# link to GitHub/GitLab/Whatever. So open it.
link=$(echo ${push_output} | grep -o 'http.*' | sed -e 's/[[:space:]]*$//')
if [ ${link} ]; then
echo ""
echo "Opening: ${GREEN}${link}${RESET}..."
python -mwebbrowser ${link}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.