Everyone has a different Git branch management strategy, bet it the popular GitFlow method or some other, home-grown concoction. But whatever branch management strategy you’re using, you’re not supposed to merge master into branches. The 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, and for those looking for an example of how to merge the GitLab master into another branch, this tutorial will suit your need.
GitLab master merge to branch options
There are actually two acceptable ways with GitLab to merge the master branch into another. Which approach you choose depends upon whether the branch you want master to merge into is protected or not.
- If you want to merge master into a branch is protected, you must perform a GitLab merge request.
- If you want to merge master into a branch that is unprotected, the easiest approach is to do a merge and push combination on the client machine.
Merge master into unprotected GitLab branch
In the current state of my GitLab repository, poor observance of proper GitFlow practices has resulted in the unprotected fun_feature branch being several commits behind master. So the first GitLab master merge into a branch will be into the fun_feature branch. We will perform this master merge entirely on the client side and not involve the GitLab gui in the process.
The steps to merge master into any branch are:
- Open a Terminal window on the client machine
- Switch to the feature branch
- Use git to merge master into the branch
- View a directory listing to validate files from master have been moved to the feature branch
- View the Git reflog to validate that a master merge commit is registered
- Push the merged feature branch back to the GitLab server
- View the feature branch in the GitLab web app to validate the merge from master to branch is reflected on the server
GitLab master merge commands
The commands to achieve this end are as follows:
git checkout fun_feature git merge master ls 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 intomaster. That sequence of events somewhat violates proper GitFlow rules, as those branches should have been merged into the release branch first, but it’s the situation I’m in. As a result, the release branch is out of sync with master, so to rectify this, a GitLab merge master into this branch is required. 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 you want 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.
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 and no conflicts arise, 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.