Me and my co-workers are working on a single project. Each one of us creates a branch for a specific task and after doing some magic we had to create a merge request to the project's main branch. Merge request will be merged after another folk did approve its changes.
One thing that bothers me, is that I have to open Gitlab and create a new merge request to the main branch, every time.

I had an idea to create a merge request in CLI, and without having to visit the Gitlab website. And thanks to the Gitlab's team it is really easy to create a merge request from CLI.

As the documentation says:

GitLab supports using Git push options to perform various actions at the same time as pushing changes.

Currently, there are push options available for:

  • Skipping CI jobs
  • Merge requests

NOTICE: You need to have Git 2.10 or newer to use push options.

Using Gitlab's push options we can create a merge request just by pushing our new branch to the remote repository. All we have to do is to add -o merge_request.create option to git push command

git push -o merge_request.create origin my-branch

Executing this command will push my-branch to the remote repository and create a new merge request from out branch to the main branch of the project.

There an option to specify the target branch of the merge request. -o merge_request.target=my-target-branch will do the magic.

git push \
    -o merge_request.create \
    -o merge_request.target=my-target-branch \
    origin my-branch 

Also, we can change the title of the merge request's

git push -o merge_request.title="<title>"

Set the description of the merge request.

git push -o merge_request.description="The description I want"

And set the merge request to remove the source branch when it’s merged.

git push -o merge_request.remove_source_branch

Gitlab push options are awesome and solved my problem. However, I'm too lazy to write all these options every time. I needed to create a script to do it with ease.

I have created a little js file to execute this command, let's call it .create-merge-request.js

var exec = require('child_process').exec;

var targetBranch = process.argv[2] || "develop"

exec("git push origin HEAD \
        -o merge_request.create \
        -o merge_request.remove_source_branch \
        -o merge_request.target=" + targetBranch,
    (error, stdout, stderr) => {
        stdout && console.log(`[stdout]\n${stdout}`);
        stderr && console.log(`[stderr]\n${stderr}`);
        if (error !== null) {
            console.log(`exec error: ${error}`);
        }
    }
);

After this I've updated the project's package.json file and added new script.

{
  "scripts": {
    "merge": "node .create-merge-request.js",
  }
}

Finally, I've created a merge request using this simple command.

yarn merge my-target-branch

NOTICE: Do not push your branch before this command. If you push your branch before this command it will not work. Git will response with Everything up-to-date and merge request will not be created