GitLab Merge Request from the CLI


Speed up your work with git by automatically creating Merge Requests for your git push

The Problem

You want to push a branch to GitLab and automatically create a Merge Request (MR) for it.

There are effectively three scenarios this can cover:

  • Create a MR in draft state with a custom title
  • Create a MR
  • Create a MR and automatically merge if CI/CD pipeline succeeds

Manually this is quite the process:

  • Push branch to origin
  • Copy link to create a MR
  • Open the link, change fields to represent wanted state and submit

The Solution

GitLab offers push options1 that allow us to instruct it to do more than just plain git push.

  • For example we can create a MR directly while pushing:

    git push
    --set-upstream origin HEAD
    -o merge_request.create
    -o merge_request.title="Awesome feature"
    
  • Or set it to be a draft:

    git push
    --set-upstream origin HEAD
    -o merge_request.create
    -o merge_request.title="Draft: Awesome feature"
    
  • Or even auto-merge after the CI/CD pipeline succeeds

    git push
    --set-upstream origin HEAD
    -o merge_request.create
    -o merge_request.title="Awesome feature"
    -o merge_request.merge_when_pipeline_succeeds
    

The Helper Scripts

For PowerShell I created a simple function to abstract this away.

Assume we are on a new branch and want to create a MR for it:

# As a draft
PS> mr "Awesome feature" -Draft

# Auto-Merging on CI/CD success
PS> mr "Awesome feature" -AutoMerge

# Just a plain old MR
PS> mr "Awesome feature" -Draft

Since I sometimes need to work with Bash too there are functions doing the same thing for it.

Under the same assumptions as above it looks like this:

# As a draft
$ dmr "Awesome Feature"

# Auto-Merging on CI/CD success
PS> mrmp "Awesome feature"

# Just a plain old MR
PS> mr "Awesome feature"

See also