Merge requests allow you to exchange changes you made to source code and collaborate with other people on the same project.
A Merge Request (MR) is the basis of GitLab as a code collaboration and version control platform. Is it simple as the name implies: a request to merge one branch into another.
With GitLab merge requests, you can:
- Compare the changes between two branches
- Review and discuss the proposed modifications inline
- Live preview the changes when Review Apps is configured for your project
- Build, test, and deploy your code in a per-branch basis with built-in GitLab CI/CD
- Prevent the merge request from being merged before it's ready with WIP MRs
- View the deployment process through Pipeline Graphs
- Automatically close the issue(s) that originated the implementation proposed in the merge request
- Assign it to any registered user, and change the assignee how many times you need
- Assign a milestone and track the development of a broader implementation
- Organize your issues and merge requests consistently throughout the project with labels
- Add a time estimation and the time spent with that merge request with Time Tracking
- Resolve merge conflicts from the UI
- Enable fast-forward merge requests
- Enable semi-linear history merge requests as another security layer to guarantee the pipeline is passing in the target branch
- Create new merge requests by email
With GitLab Enterprise Edition, you can also:
- View the deployment process across projects with Multi-Project Pipeline Graphs (available only in GitLab Enterprise Edition Premium)
- Request approvals from your managers (available in GitLab Enterprise Edition Starter)
- Squash and merge for a cleaner commit history (available in GitLab Enterprise Edition Starter)
- Analise the impact of your changes with Code Quality reports (available in GitLab Enterprise Edition Starter)
A. Consider you are a software developer working in a team:
- You checkout a new branch, and submit your changes through a merge request
- You gather feedback from your team
- You work on the implementation optimizing code with Code Quality reports (available in GitLab Enterprise Edition Starter)
- You build and test your changes with GitLab CI/CD
- You request the approval from your manager
- Your manager pushes a commit with his final review, approves the merge request, and set it to merge when pipeline succeeds (Merge Request Approvals are available in GitLab Enterprise Edition Starter)
- Your changes get deployed to production with manual actions for GitLab CI/CD
- Your implementations were successfully shipped to your customer
B. Consider you're a web developer writing a webpage for your company's:
- You checkout a new branch, and submit a new page through a merge request
- You gather feedback from your reviewers
- Your changes are previewed with Review Apps
- You request your web designers for their implementation
- You request the approval from your manager (available in GitLab Enterprise Edition Starter)
- Once approved, your merge request is squashed and merged, and deployed to staging with GitLab Pages (Squash and Merge is available in GitLab Enterprise Edition Starter)
- Your production team cherry picks the merge commit into production
Merge requests per project
View all the merge requests within a project by navigating to Project > Merge Requests.
When you access your project's merge requests, GitLab will present them in a list, and you can use the tabs available to quickly filter by open and closed. You can also search and filter the results.
Merge requests per group
View all the merge requests in a group (that is, all the merge requests across all projects in that group) by navigating to Group > Merge Requests. This view also has the open, merged, and closed merge request tabs, from which you can search and filter the results.
Authorization for merge requests
There are two main ways to have a merge request flow with GitLab:
- Working with protected branches in a single repository
- Working with forks of an authoritative project
Cherry-pick any commit in the UI by simply clicking the Cherry-pick button in a merged merge requests or a commit.
Semi-linear history merge requests
A merge commit is created for every merge, but the branch is only merged if a fast-forward merge is possible. This ensures that if the merge request build succeeded, the target branch build will also succeed after merging.
Navigate to a project's settings, select the Merge commit with semi-linear history option under Merge Requests: Merge method and save your changes.
Fast-forward merge requests
If you prefer a linear Git history and a way to accept merge requests without creating merge commits, you can configure this on a per-project basis.
Merge when pipeline succeeds
When reviewing a merge request that looks ready to merge but still has one or more CI jobs running, you can set it to be merged automatically when CI pipeline succeeds. This way, you don't have to wait for the pipeline to finish and remember to merge the request manually.
Resolve discussion comments in merge requests reviews
Keep track of the progress during a code review with resolving comments. Resolving comments prevents you from forgetting to address feedback and lets you hide discussions that are no longer relevant.
When a merge request has conflicts, GitLab may provide the option to resolve those conflicts in the GitLab UI.
Create new merge requests by email
You can create a new merge request by sending an email to a user-specific email address. The address can be obtained on the merge requests page by clicking on a Email a new merge request to this project button. The subject will be used as the source branch name for the new merge request and the target branch will be the default branch for the project. The message body (if not empty) will be used as the merge request description.
GitLab implements Git's powerful feature to revert any commit with introducing a Revert button in merge requests and commit details.
Merge requests versions
Every time you push to a branch that is tied to a merge request, a new version of merge request diff is created. When you visit a merge request that contains more than one pushes, you can select and compare the versions of those merge request diffs.
Work In Progress merge requests
To prevent merge requests from accidentally being accepted before they're completely ready, GitLab blocks the "Accept" button for merge requests that have been marked as a Work In Progress.
Merge request diff file navigation
The diff view has a persistent dropdown for file navigation. As you scroll through diffs with a large number of files and/or many changes in those files, you can easily jump to any changed file through the dropdown navigation.
Ignore whitespace changes in Merge Request diff view
If you click the Hide whitespace changes button, you can see the diff without whitespace changes (if there are any). This is also working when on a specific commit page.
Tip: You can append
?w=1while on the diffs page of a merge request to ignore any whitespace changes.
Live preview with Review Apps
If you configured Review Apps for your project, you can preview the changes submitted to a feature-branch through a merge request in a per-branch basis. No need to checkout the branch, install and preview locally; all your changes will be available to preview by anyone with the Review Apps link.
Here are some tips that will help you be more efficient with merge requests in the command line.
Note: This section might move in its own document in the future.
Checkout merge requests locally
A merge request contains all the history from a repository, plus the additional commits added to the branch associated with the merge request. Here's a few tricks to checkout a merge request locally.
Please note that you can checkout a merge request locally even if the source project is a fork (even a private fork) of the target project.
Checkout locally by adding a git alias
Add the following alias to your
[alias] mr = !sh -c 'git fetch $1 merge-requests/$2/head:mr-$1-$2 && git checkout mr-$1-$2' -
Now you can check out a particular merge request from any repository and any
remote. For example, to check out the merge request with ID 5 as shown in GitLab
upstream remote, do:
git mr upstream 5
This will fetch the merge request into a local
mr-upstream-5 branch and check
Checkout locally by modifying
.git/config for a given repository
Locate the section for your GitLab remote in the
.git/config file. It looks
[remote "origin"] url = https://gitlab.com/gitlab-org/gitlab-ce.git fetch = +refs/heads/*:refs/remotes/origin/*
You can open the file with:
git config -e
Now add the following line to the above section:
fetch = +refs/merge-requests/*/head:refs/remotes/origin/merge-requests/*
In the end, it should look like this:
[remote "origin"] url = https://gitlab.com/gitlab-org/gitlab-ce.git fetch = +refs/heads/*:refs/remotes/origin/* fetch = +refs/merge-requests/*/head:refs/remotes/origin/merge-requests/*
Now you can fetch all the merge requests:
git fetch origin ... From https://gitlab.com/gitlab-org/gitlab-ce.git * [new ref] refs/merge-requests/1/head -> origin/merge-requests/1 * [new ref] refs/merge-requests/2/head -> origin/merge-requests/2 ...
And to check out a particular merge request:
git checkout origin/merge-requests/1