GitLab: generating URL that can be used for Merge Request from fork to upstream

The forked workflow is popularized by the Open Source community where your personal contributions are made by having your own personal fork of a repository and pushing a GitLab Merge Request to a central repository.

A GitLab Merge Request can be submitted from the web UI by clicking on “Merge requests” and manually selecting the source and target branches, title, squash options, etc.  But this manual selection is tedious.

A URL with syntax like below will provide reasonable defaults for a new Merge Request.

# setup variables
owner=fabianlee
repo_name=myrepo123
title_url_encoded="my+title"

# URL syntax
https://gitlab.com/$owner/$repo_name/-/merge_requests/new?merge_request[source_branch]=main&merge_request[target_branch]=main&merge_request[force_remove_source_branch]=false&merge_request[squash]=true&merge_request[squash_on_merge]=true&merge_request[title]=$title_url_encoded

Assuming that you have ‘origin’ and ‘upstream’ remotes defined, you can use my script show-gitlab-merge-request-url.sh to automatically populate the repository URL, current branch, and title of the new merge request based on the last commit message.

Merge Request directly from console

If you have installed the ‘glab‘ utility, you can create a Merge Request directly from the console.

glab mr create --fill --squash-before-merge --source-branch $current_branch --target-branch $current_branch --head $origin_owner_repo --repo $upstream_owner_repo -y

Read my article on glab for example parameter values.

 

REFERENCES

gitlab docs, create MR with api

fabianlee.org, glab utility

unix.stackexchange, several methods for URL encoding string from Bash

gitlab.com, install gh CLI tool

gitlab.com issue, gitlab web UI page does not honor squash query param

stackoverflow.com, gitlab web UI page does not set squash via param

github.com issues – known problem with merge request creation and “you must be on a different branch”

NOTES

Create fork of one of your own repo (NOTE: fork of your own repo is possible with gitlab, but not with github)

glab repo fork https://gitlab.com/fabianlee/mytest123 --name myfork456 --path myfork456 --clone