Updated on 2025-09-23 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.

MR pre-merge has the following advantages:

  • 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

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.

    git fetch origin +refs/merge-requests/${repo_MR_iid}/merge:refs/remotes/origin/${repo_MR_iid}/merge
    • As shown in the following figure, 10 under the merge request title is repo_MR_iid.
      Figure 1 MR IID
    • merge-requests/${repo_MR_iid}/merge indicates the temporary merge node.

  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 merge request to the local branch.

    git fetch origin +refs/merge-requests/${repo_MR_iid}/head:refs/remotes/origin/${repo_MR_iid}/head
    • As shown in the following figure, 10 under the merge request title is repo_MR_iid.
    Figure 2 MR IID
    • merge-requests/${repo_MR_iid}/head indicates the source branch of the merge request.

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

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