Create Pull Request Script

Posted on March 19, 2025 by Michael Keane Galloway

A long while back I posted about my favorite git trick. I’ve continued to refine it since that post. I had two issues that I wanted to solve: I wanted to have a command that would open the repository in the browser without having to take any other action, and I wanted to be able to open the pull request form to the correct branch. The latter desire stemmed from a large monolith that could cause browser timeouts while switching target branches. With some experimentation, I found that I could open the pull request form with the target branch and it would just work. That eventually led me to the following one line power shell script:

git remote -v | \
 ? { $_.contains("fetch") } | \
 % { start-process "$($_.split()[1])/pullrequestcreate?sourceRef=$(git branch | \ 
 ? {$_.contains('*')} | \
 % {$_.replace('*', '').trim()})&targetRef=release"}
  1. Get the remote URLs for the repository with the verbose switch. If you have more than one remote this would have to be changed to select the appropriate remote. I don’t commonly work with multihomed repositories so that’s not a problem for me.
  2. Use the where-object cmdlet to get the line that contains the fetch URL for the remote. This is the URL that git will use to get changes from the remote server. Since this is built with a single server in mind we’re just arbitrarily picking the fetch URL.
  3. Use the for-object cmdlet to execute a browser with the fetch URL interpolated into the URL for the pull request form in Azure DevOps.
  4. As part of the execution above, I use a piped command to get the current branch as the source reference.

Unfortunately this one liner approach would only work for the release branch. If I wanted to merge into main, I would have to alter this to leave the target blank. Wanting a more ergonomic way of changing my target branch eventually led me to create the following:

param($targetBranch='main')
$branch = git branch | ? {$_.contains('*')} | % {$_.replace('*', '').trim()};
$repoUrl = git remote -| ? { $_.contains("fetch") } | % { $_.split()[1] };
$prUrl = "$($repoUrl)/pullrequestcreate?sourceRef=$($branch)&targetRef=$($targetBranch)";
start-process $prUrl;
  1. This script takes a target branch as a parameter. This ends up translating into a command line switch.
  2. I then have a separate line getting the current branch with a piped command like I made in the last script.
  3. I then assign a similar git remote command to a variable for the repository URL.
  4. I use string interpolation to put together the pull request URL. This version of the script defaults the target to main in the parameters. If you’re working with an older repository, you might have to remember to supply master.

I have the above code block as a script create-pullrequest.ps1 in a bin folder on my PowerShell path variable. That way I can quickly make a pull request to main by just invoking the script or key in create-pullrequest -targetBranch=release and create release branches. This script has been incredibly useful; especially when I have 15 repositories that need to merged for release at the end of the sprint.