How to merge master into any branch in GitLab by examples

Every developer has a different Git branch management strategy, be it the popular GitFlow method or some other, home-grown concoction. But whatever branch management strategy you use, developers aren’t supposed to merge master into branches. In fact, the exact opposite is supposed to happen.

And, only a few branches should be allowed to merge into master, namely the hotfix and releases branch. But, there are always exceptions to the rule. This tutorial will show developers an example of how to merge the GitLab master into another branch.

GitLab merge master to branch options

There are two acceptable ways with GitLab to merge the master branch into another. Which approach a developer chooses will depend whether the branch they want master to merge into is protected or not.

  1. If the developer wants to merge master into a branch that’s protected, they must perform a GitLab merge request.
  2. If the developer wants to merge master into a branch that’s unprotected, the easiest approach is to do a merge and push combination on the client machine.
gitlab delete remote branch

The remote GitLab branch to delete is name feature_fun.

Merge master into unprotected GitLab branch

In the current state of my GitLab repository, I haven’t observed many GitFlow best practices and as a result, it has left the unprotected fun_feature branch several commits behind the master. The first GitLab merge master into a branch will be into the fun_feature branch. We will perform this merge master entirely on the client side and won’t involve the GitLab GUI in the process.

The steps to merge master into any branch are:

  1. Open a Terminal window on the client machine
  2. Switch to the feature branch
  3. Use git to merge master into the branch
  4. View a directory listing to validate files from master have been moved to the feature branch
  5. View the Git reflog to validate that a master merge commit is registered
  6. Push the merged feature branch back to the GitLab server
  7. View the feature branch in the GitLab web app to validate the merge from master to branch is reflected on the server

GitLab merge master commands

A developer can use the following commands to merge master into any branch in GitLab:

git checkout fun_feature
git merge master
git reflog
git push origin fun_feature

GitLab master to branch merge request

In a previous GitLab tutorial, both the develop branch and the hotfix branch was merged into master. That sequence of events somewhat violates proper GitFlow rules, because those branches should’ve been merged into the release branch first. As a result, the release branch is out of sync with master. I can rectify this problem with a GitLab merge master into this branch. When the merge is complete, the release and master branch will be in sync.

Since the release branch is protected, the process is a little different. If a developer wants to merge the GitLab master into a protected branch, the proper approach is to perform a merge request. Simply log into the GitLab web app, choose the option to create a GitLab merge request, specify the master branch as the source and the protected branch as the target of the merge.

gitlab master merge

The merge request can be used to have GitLab merge master in a branch that is protected.

After the merge request is submitted, the assignee will be notified of the request the next time they log in. If the merge request is approved without conflict, the GitLab master will merge into the branch, and both the master and release branches will be in sync.

The source code and sample project used for these examples can be found on the gitlab-made-easy project page on GitLab.

Check out this how to use GitLab tutorial to learn more.

App Architecture
Software Quality
Cloud Computing