57

I've just started using GitLab, and have created a set of issues, in order to keep an overview of what needs to be done for my application. I was wondering if it was possible to create a branch from these issues, such that the branch and issues are linked, similar as in jira and Stash from atlassian?

JonasN89
  • 1,386
  • 2
  • 11
  • 23

4 Answers4

79

If you create a branch with the name <issue-number>-issue-description and push that branch to gitlab, it will automatically be linked to that issue. For instance, if you have an issue with id 654 and you create a branch with name 654-some-feature and push it to gitlab, it will be linked to issue 654.

Gitlab will even ask you if you want to create a merge request and will automatically add Closes #654 to the merge request description which will close issue 654 when the merge request is accepted.

Also if you go to a given issue page on gitlab, you should see a New Branch button which will automatically create a branch with a name of the form <issue-number>-issue-description.

Jawad
  • 4,457
  • 1
  • 26
  • 29
  • 4
    I was hoping to see a New Branch button, but either its not there or I can't find it. Do you know where it is placed? – JonasN89 Apr 09 '17 at 11:53
  • never mind found the create new branch. It seems like Gitlab created a branch for me for the first issue, without realizing it myself. – JonasN89 Apr 09 '17 at 12:06
  • 3
    Regarding the missing Create Branch button, see @Robert Echlin's answer. – florisla Jan 26 '18 at 12:24
  • 2
    @JonasN89 you should consider changing the accepted answer to Robert Echlin's answer because that exactly answers your question. – Zoltán Apr 04 '18 at 14:49
  • 2
    An observation: the "Create Pull Request for Issue" Button is only visible once an Issue is Non-Confidential. – Michael Kargl Jul 14 '19 at 17:23
  • Can one branch be linked to more than one issue in a case where the branch addresses those issues? – BrunoElo Oct 27 '22 at 11:52
49

On the Issue screen, you see a green button labeled "Create merge request", with a down-arrow to its right.

That's not a button, that's a drop down list of buttons.

  • Click on the down arrow
  • Choose "Create branch"
  • Click on "Create branch"
  • A branch is created from the issue number, plus the title of the branch
    • For example, my issue #2, with title "Test repoSearch with no repos" will have a branch called:
    • 2-test-reposearch-with-no-repos
    • Notice that it changes case to all-lower-case
Thinsky
  • 4,226
  • 3
  • 13
  • 22
Robert Echlin
  • 623
  • 5
  • 6
  • 4
    That button is not displayed for me. Does it have to do with my branch structure (Git Flow) or because it is a fork of a different project? – molerat Aug 28 '18 at 14:19
  • 4
    Is it posible for GitLab to recognise other name format? For instance, create a branch called `feature/2` by default instead of `2-the-title`. And, would they be linked by GitLab? That would be a must feature IMHO. – Roger Campanera Feb 08 '19 at 12:26
  • @molerat I'm having the issue where it's not showing for me either in the current project. In one project, I have 2 buttons. Show Activity and Create Merge Request. In the other, I only have Show Activity – ThinkDigital Jan 01 '20 at 22:24
  • same here, on some issues the button is displayed while not in others; cannot understand which criteria gitlab uses to show it or not – fudo Sep 10 '20 at 09:20
  • The repository must not be empty and the 'repository' feature should be enabled in the project settings (Settings > General > Visibility, project features, permissions). Tested on Gitlab 13.8.7 / Docker. – Hendrik Pilz Apr 14 '21 at 11:47
  • 1
    See this [issue](https://stackoverflow.com/questions/69262304/gitlab-create-branch-and-merge-request-from-issue/69979349#69979349) regarding the Merge Request button not displaying on the Issue screen. – Jubilsoft-Scott Nov 15 '21 at 19:05
  • @RogerCampanera it seems the only option for Gitlab is ```feature/2-``` then when you do a merge request add ```#2``` in the comment box and it will link then, you could just do a MR right from the start to link it. From what I can tell on Gitbhub you can link pull-request to ```feature/2-``` but doesn't have the autolinking of ```2-``` like GitLab – Timothy L.J. Stewart Jan 24 '22 at 22:54
  • My issue was due to my project being a fork of another repo. I had to follow the recommendation from @Jubilsoft-Scott above to "Remove Project Fork Relationship" and the issue was resolved. I could then create a branch (and MR) from the issue as desired. – Ryan S Dec 13 '22 at 19:36
2

TLDR: do a merge request add #2 in the title and/or in the comment box and/or the commit message and it will link the issue to the branch and commit, you could just do a MR right from the start to link it.

It seems the only option for Gitlab is name your branch following this format: <issue-number-some-branch> like 2-bad-ai this will autolink the branch to the issue.

However, I organize my branches so they live nicely in the .git/ref/heads folder structure like this feature/2-<some-branch> then when you do a merge request add #2 in the title and/or in the comment box and/or the commit message and it will link the issue to the branch and commit, you could just do a MR right from the start to link it.

$ ls .git/refs/heads/; ls .git/refs/remotes/upstream/
2-bad-ai  dev  feature/  hotfix/  master  release/
2-bad-ai  dev  feature/  hotfix/  master  release/ 

I'd much rather have feature/2-bad-ai in the above output...

From what I can tell on Github you can link pull-request to feature/2-<some-branch> but doesn't have the autolinking of 2-<some-branch> like GitLab

Timothy L.J. Stewart
  • 1,584
  • 19
  • 15
1

2017:

If you create a branch with the name -issue-description and push that branch to GitLab, it will automatically be linked to that issue.

This is now customizable, with GitLab 15.6 (November 2022):

Configure default names for branches created from issues

Define a custom template for naming branches created from issues. The previous setting {issue ID}-{issue-title-hyphenated} remains the default.

To define a custom template for your project, go to Repository Settings > Branch defaults.

https://about.gitlab.com/images/15_6/configure_default_names_for_branches_created_from_issues.png -- Configure default names for branches created from issues

See Documentation and Issue.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250