IT IS HERE! Get Smashing Node.JS on Amazon Kindle today!
Show Posts
← Back to homepage

I created a tiny yet useful shell script that opens a browser window on the GitHub “new pull request” interface.

Add the following to your bash profile and run it as pr. It will detect the user, repository and branch automatically.

function pr () {
  local repo=`git remote -v | grep -m 1 "(push)" | sed -e "s/.*[:/]\(.*\)\.git.*/\1/"`
  local branch=`git name-rev --name-only HEAD`
  echo "... creating pull request for branch \"$branch\" in \"$repo\""

export -f pr


fwg said

Put it in ~/bin/git-pr (or some other dir in $PATH) with a shebang and
$ git pr
off you go.

Jeff said

This is very handy!

If you’re using Git for Windows, you can replace the ‘open’ command with ‘start’

Guillermo Rauch said

@ftw, @Jeff
Two great tips :) Thanks

Nathan Rajlich said

For repos with more than 1 remote, it wasn’t working for me. So I added a call to `head -n 1` to get only the first result.

git remote -v | grep push | head -n 1 | sed -e “s/.*[:/]\(.*\)\.git.*/\1/”

Nice script!

Kevin said

I just tried this and it works great. Thanks for sharing.

Guillermo Rauch said

Thanks! Made the match slightly more solid. It can still submit the pull request to the wrong repo in the case of multiple push remotes, so I guess it doesn’t totally fit your use case still.

svs said does the same but with a bit more functionality. You can push to any branch on the current repo or even the parent, and you can choose which branch to push to interactively.

pull –help prints a usage message.


Your thoughts?

About Guillermo Rauch:

CTO and co-founder of LearnBoost / Cloudup (acquired by Automattic in 2013). Argentine living in SF.