When ready working on a feature
branch
in a project that
uses GitHub, GitLab or anything else like them, one generally do git push -u origin <branch_name> and then goes to the web interface to open a pull
request. Pretty much all of these platforms provide the URL to go in order to
spare a few clicks. For instance:
❯ git push -u origin baba
Enumerating objects: 1, done.
Counting objects: 100% (1/1), done.
Writing objects: 100% (1/1), 181 bytes | 181.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: Create a pull request for 'baba' on GitHub by visiting:
remote: https://github.com/user/project_name/pull/new/baba
remote:
To github.com:user/project_name.git
* [new branch] baba -> baba
A few years ago Rado Stankov wrote a great post about a few git aliases and scripts he uses to automate things: Git Tricks . I took literally all of the tricks he shows there in my dotfiles and strongly suggest for you to do the same.
One of tricks there is a git-pull-request scripts which allows automating
that clicking on the provided link with a git pull-request command. Over time
however, I’ve started using GitLab and this trick no longer works for that, as
it assumes you’re pushing into a GitHub repository. So, I had to modify that
script in order to be agnostic of the hosting you’re using and simply open
whatever URL the upstream provdes on push (which to be fair, might be a bit
risky, if you’re pushing to a shady git hosting, so use it with care):
#!/bin/sh
current_branch=$(git rev-parse --abbrev-ref HEAD)
git push -v --set-upstream origin "$current_branch" $2 2>&1 | while IFS= read -r line
do
echo "$line"
url=$(echo "$line" | grep "remote:" | grep -Eo '(http|https)://[^ ]+')
if [ -n "$url" ]
then
xdg-open "$url" > /dev/null
fi
done
Note that as a linux user, I’ve changed open to xdg-open. If you’re using
macOS, might what to do s/xdg-open/open/ over this.
If this is saved in a file named git-pull-request with execute permissions
(+x) at some of the directories listed in your $PATH, running git pull-request will push your current branch upstream and output exactly the
same as above, but also open the provided link in your default web browser
(presumably in a new tab).