How to checkout merge request locally, and create new local branch?

GitGitlabGit Merge

Git Problem Overview


I have GitLab repository there and I need to test every merge request locally, before merging to the target branch.

How can I pull/fetch merge request as a new branch?

Git Solutions


Solution 1 - Git

  1. Pull merge request to new branch

    git fetch origin merge-requests/REQUESTID/head:BRANCHNAME

i.e git fetch origin merge-requests/10/head:file_upload 2. Checkout to newly created branch

`git checkout BRANCHNAME`

i.e ( git checkout file_upload)

OR with single command

git fetch origin merge-requests/REQUESTID/head:BRANCHNAME && git checkout BRANCHNAME

i.e git fetch origin merge-requests/18/head:file_upload && git checkout file_upload

Solution 2 - Git

This is also documented in GitLab online documentation : https://docs.gitlab.com/ee/user/project/merge_requests/reviewing_and_managing_merge_requests.html#checkout-merge-requests-locally-through-the-head-ref

They supply this script (git alias) :

[alias]
    mr = !sh -c 'git fetch $1 merge-requests/$2/head:mr-$1-$2 && git checkout mr-$1-$2' -

Then you can use this command :

> git mr origin 4

So a new local branch mr-origin-4 will be created.

Solution 3 - Git

You can also add the line

fetch = +refs/merge-requests/*/head:refs/remotes/origin/merge-requests/*

to your .git/config to have git fetch fetch all merge requests.

Solution 4 - Git

There is a Check out branch button in GitLab.

enter image description here


Then you can copy comments from Step 1. Fetch and check out the branch for this merge request.

git fetch <repo> <branch>
git checkout -b <branch>

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLokinder Singh ChauhanView Question on Stackoverflow
Solution 1 - GitLokinder Singh ChauhanView Answer on Stackoverflow
Solution 2 - GitGuillaume HustaView Answer on Stackoverflow
Solution 3 - GitTobias KienzlerView Answer on Stackoverflow
Solution 4 - GitPenny LiuView Answer on Stackoverflow