Published 2021-04-03
The story begins with a developer starting to feel bored and tiring about the development workflow and trying his best to at least make a SINGLE part of the workflow better & feeling more productive.
If you're tired of creating merge-request using the Gitlab website and you want to do it in a faster & simple way. Then this post could be what you're looking for. Without further ado, let's start.
Before we dive deep into the step-by-step guide, let's define the outcome we would like to achieve 1st.
This would be the ultimate product we would like to have.
Create a Merge Request using one line of command
In fact, Gitlab provides push options to do this using a single command. You can refer to the full documentation here.
merge_request.create
allows us to create a merge requestmerge_request.target=master
allows us to set the target branch for the merge request to mastermerge_request.remove_source_branch
allows us to set remove the source branch when the merge request is merged.After we understood what each options do, here is the full command:
# Here will be the format of the command line
git push -o merge_request.create -o merge_request.target=master -o merge_request.remove_source_branch $upstream_remote_name $local_branch_name
# Sample run command
git push -o merge_request.create -o merge_request.target=master -o merge_request.remove_source_branch origin feature/payment-integration
We have achieved the outcome that we want. But yet, writing this long command in the above is even worst compared to creating the merge requests in the Gitlab website (My personal feeling 😅 ).
Luckily, we can make this better and shorter using Git alias. Here is how I defined my Git alias in my .gitconfig
file.
[alias]
bn = "!git rev-parse --abbrev-ref HEAD"
mrtd = "!git push -o merge_request.create -o merge_request.target=development -o merge_request.remove_source_branch origin $(git bn)"
mrts = "!git push -o merge_request.create -o merge_request.target=staging -o merge_request.remove_source_branch origin $(git bn)"
mrtp = "!git push -o merge_request.create -o merge_request.target=master -o merge_request.remove_source_branch origin $(git bn)"
I have 4 Git alias:
In short, this long command have been simplified from:
git push -o merge_request.create -o merge_request.target=master -o merge_request.remove_source_branch origin feature/payment-integration
to:
git mrtd
: Create Merge Request to Development Branchgit mrts
: Create Merge Request to Staging Branchgit mrtp
: Create Merge Request to Production BranchI personally really enjoy the outcome of it because it really saves a lot of my time in creating merge requests. With those tiny little time you save every day, it would be really a substantial amount of time you save along your career life.
I hope you enjoy this post and I will see you in next article.
This section summarized resources I discovered during the research for this topic.