Skip to content

Instantly share code, notes, and snippets.

@kfox
Last active September 9, 2022 15:31
Show Gist options
  • Save kfox/e006abfb45f8d0e4194aafc4347489e2 to your computer and use it in GitHub Desktop.
Save kfox/e006abfb45f8d0e4194aafc4347489e2 to your computer and use it in GitHub Desktop.
Bash function to open a GitHub pull request from the command line
# in a git repo, compare your branch to another branch on github.com
# and optionally create a pull request
function gpr {
local repo
local branch
local title
repo=$(git ls-remote --get-url 2>/dev/null)
branch=$(git branch --no-color --contains HEAD 2>/dev/null | awk '{ print $2 }')
title=$(git log -1 --pretty=%B | tr -d '\n')
if [[ $repo == *"git@github.com"* ]]; then
repo=${repo/git@github.com:/https:\/\/github.com/}
repo=${repo/.git/}
fi
if [ -n "${repo}" ] && [ -n "${branch}" ]; then
local url="${repo}/compare/${branch}?expand=1&title=${title}"
echo "${url}" | pbcopy
open "${url}"
else
echo "Not in a git repository."
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment