Updated on 2025-07-24 GMT+08:00

Pre-Merging an MR

Overview

Pre-merging an MR generates a temporary merge node in the repository. When the code in an MR has not been merged, you can download the pre-merge code via plugin scripts such as custom webhooks or pipelines for code building.

Advantages of pre-merge

In the MR merging process involving dozens or hundreds of servers for code building, the local client pre-merging result may be different from that generated by the server and yield inaccurate build. Enabling MR pre-merge can solve this real-time issue, as the build script commands are simpler compared to local pre-merging scripts, making it easier for developers to get started.

Constraints

  • Enable Pre-merge.
  • You need to have the set permission.

Example

The following example shows the difference between the scripts with Pre-merge enabled and unenabled. The former one is more simple and efficient.

  • Pre-merge: When an MR is created, a temporary merge node will be generated on the server. You can download the code that has been temporarily merged. Steps are provided as follows:
  1. Initialize the local repository. In the command, repo_url indicates the address of the target repository.

    git init  
    git remote add origin ${repo_url}

  2. Pulls the temporary merge node from the server to the local branch. As shown in the following figure, 10 under the merge request header is displayed as repo_MR_iid. merge-requests/${repo_MR_iid}/merge indicates the temporary merge node, and ${repo_MR_iid}/merge indicates the local branch.

    git fetch origin +refs/merge-requests/${repo_MR_iid}/merge:refs/remotes/origin/${repo_MR_iid}/merge
    Figure 1 MR IID

  3. Check out the branch and the pre-merged code is obtained.

    git checkout ${repo_MR_iid}/merge

If MR pre-merge is not enabled, you need to download the code of the MR source and target branches on the client and merge on your build executor. To update the information, perform the following operations:

  1. Initialize the local repository. In the command, repo_url indicates the address of the target repository.

    git init  
    git remote add origin ${repo_url}

  2. Pull and check the target branch of the MR. The repoTargetBranch represents the target branch of the MR.

    git fetch origin +refs/heads/${repoTargetBranch}:refs/remotes/origin/${repoTargetBranch}
    git checkout ${repoTargetBranch}

  3. Pull the source branch of the MR to the local branch. In the following figure, 10 under the MR header is shown as repo_MR_iid. merge-requests/${repo_MR_iid}/head indicates the source branch of the MR, and ${repo_MR_iid}/head the local branch.

    git fetch origin +refs/merge-requests/${repo_MR_iid}/head:refs/remotes/origin/${repo_MR_iid}/head
    Figure 2 MR IID

  4. Merge locally and the pre-merged code is generated.

    git merge refs/remotes/origin/${repo_MR_iid}/head --no-edit