Creating a Gitlab Merge Request from the Command Line
I quite like working with Merge Requests for Code Reviews and for providing a Feature Changelog.
Additionally, I tend to create them quite early. Sometimes even when creating a new branch.
This allows me to assign other people who might be interested on the progress of the Feature I’m working on.
Neatly, Gitlab already shows you a Link when pushing to a branch.
This URL creates a MR for the branch feature/my-branch.
https://gitlab.com/GROUP/REPO/-/merge_requests/new?merge_request%5Bsource_branch%5D=feature%2Fmy-branch
Another way is using Git Push Options.
Gitlab Push Options
When pushing a commit to your Gitlab repository, you can specify some useful options. Note that this only passes variables to branch pipelines and not merge request pipelines.
Additionally, for the case of already having pushed, I created a script to create the MR.
Shell Script to Create a Gitlab MR for the Current Branch
What it does:
- Gets the URL of the repository with
git remote get-url origin
- Get the current branch name with
git rev-parse --abbrev-ref HEAD
- Encodes it properly, using my urlencode.sh script.
- Generates the correct Gitlab URL to create the MR
- Opens the link with
xdg-open
Here’s the version I currently use.