69

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?

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Lokinder Singh Chauhan
  • 2,326
  • 1
  • 20
  • 23

4 Answers4

94
  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

Lokinder Singh Chauhan
  • 2,326
  • 1
  • 20
  • 23
28

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.

Guillaume Husta
  • 4,049
  • 33
  • 40
16

You can also add the line

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

to your .git/config (section [remote "origin"] or replace origin with your remote's name) to have git fetch fetch all merge requests. This line is in addition after any already existing fetch line (the + causes this to be added, not replacing).

Tobias Kienzler
  • 25,759
  • 22
  • 127
  • 221
  • How to get particular patch within the MR? – Kanagavelu Sugumar Jan 26 '21 at 09:13
  • @KanagaveluSugumar You mean `git cherrypick`? Otherwise please [ask a new question](https://stackoverflow.com/questions/ask) – Tobias Kienzler Jan 27 '21 at 18:05
  • In gerrit i can fetch particular patch set with command like `git fetch ssh://user@gerrit.it.**.com:29418/OneSearch/controller/xx refs/changes/30/1228430/3 && git checkout FETCH_HEAD` where `3` is the patch set pushed to the review 1228430. Is there any such option in gittlab MR? – Kanagavelu Sugumar Jan 28 '21 at 06:21
  • So I dont want latest change/patch in the MR, instead a particular patch set after the review created. e.g. Say once MR is created if user has pushed multiple changes from the initial code. Each code change push will be a revision/patch right? in that i need to download locally a particular change/patch set to test. – Kanagavelu Sugumar Jan 28 '21 at 06:30
  • @KanagaveluSugumar I'm afraid I don't know, you'll have to ask a separate question instead, maybe someone else knows – Tobias Kienzler Jan 28 '21 at 11:50
  • /git/config has multiple sections. Which section should this line be added to? And in that section, there may already be a line that starts with "fetch =". What do do in that case? – Mike Nakis May 30 '23 at 10:36
  • @MikeNakis Edited for clarity – Tobias Kienzler May 30 '23 at 15:11
11

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

Penny Liu
  • 15,447
  • 5
  • 79
  • 98