Bitbucket/GitHub: Create pull request from command line
Create a GitHub pull request from command line
You may have noticed that when you push a new branch to a GitHub repository, you get a handy URL to create a corresponding pull request:
$ git checkout -b test
Branch 'test' set up to track local branch 'master' by rebasing.
Switched to a new branch 'test'
$ git push -u origin test
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'test' on GitHub by visiting:
remote: https://github.com/proinsias/proinsias.github.io/pull/new/test
remote:
To github.com:proinsias/proinsias.github.io.git
* [new branch] test -> test
Branch 'test' set up to track remote branch 'test' from 'origin' by rebasing.
You can of course visit the URL from the command line:
open https://github.com/proinsias/proinsias.github.io/pull/new/test
Create a GitHub pull request using gh
If you want more control over this process from the command line, check out
GitHub’s official gh CLI. For example, gh enables
you to create a pull request with a pre-populated title and description:
gh pr create \
--assignee proinsias \
--web \
--label work-in-progress \
--title "My title" \
--body "My description."
Update: this post originally recommended GitHub’s
hub tool for this workflow. hub has since
been superseded by GitHub’s own official CLI, gh (first released in 2020),
which is now the recommended tool. The old hub invocation was:
hub pull-request \
--assign proinsias \
--browse \
--labels work-in-progress \
--message "My title" \
--message "My description." \
Create a Bitbucket pull request from command line
The following script (based on bitbucket-pr and updated for python3) achieves something similar for Bitbucket repos:
#!/bin/bash
project=${PWD##*/}
rawBranch=$(git rev-parse --abbrev-ref HEAD)
org=$(git config --get remote.origin.url | grep -Eo "(\w*)/$project.git\/?$" | cut -d/ -f1)
branch=$(python -c "import urllib.parse; print(urllib.parse.quote_plus('''$rawBranch'''))")
if [[ $org = *[!\ ]* ]]
then
echo "Opening browser..."
else
echo "Not in a bitbucket repo"
exit
fi
open "https://bitbucket.org/${org}/${project}/pull-requests/new?source=${branch}"
Leave a comment