54

In JIRA connected with STASH you can create a feature branch for an issue using the button 'create branch'. (That is nice to track the commits in this issue.)

If a developer started working but did not know that there is such an issue he did not click the 'create branch'.

Is there any possibility to assign an existing git branch to an issue?

550
  • 1,070
  • 1
  • 13
  • 28

7 Answers7

47

ex-Stash developer here.

Yes and no. Creating the branch though the UI is just a convenience. The important thing is that the name contains the JIRA key. If only one developer is working on the branch, it's fairly easy to just rename (delete + add) a branch with the appropriate name.

git checkout old-branch
git push -u origin old-branch:JIRAKEY-1234-something
git push origin :old-branch

Does that help?

charleso
  • 1,816
  • 14
  • 12
  • 10
    Perhaps, this was the case in 2014. Right now, the branch can be named anything. The only way to link a branch to a JIRA issue is to create that branch via the JIRA issue interface. To link an existing branch, one must delete it from Stash (now BitBucket Cloud), use the JIRA interface to re-create it, then push/merge into it. – XedinUnknown Nov 18 '15 at 14:59
  • 6
    Referring to the earlier comment about the solution not working anymore. Tried this today, and it absolutely does work – Kedar Mar 14 '16 at 15:50
  • charleso: what is "something" and how does it create "the appropriate name"? What about using the same branch for multiple tickets? XedinUnknown yes, the branch can be named anything. Does this mean two Jira tickets can't refer to same branch? @Kedar what were the steps you used? I don't feel the original answer had enough info – Andrew Wolfe Mar 18 '16 at 14:44
  • 1
    @AndrewWolfe The branch name should contain the JIRA ticket in its name. If my JIRA issue is "SM-100 Shunt the deuterium" and the branch is "get-the-warp-drive-ready" . If I want my branch to be tied to SM-100 I need to change the branch name. So I move my branch to a new one and then delete my branch. So from command line - git checkout get-the-warp-drive-ready git push -u origin get-the-warp-drive-ready:SM-100-Shunt-the-deuterium git push origin :get-the-warp-drive-ready Since JIRA id is unique, I guess only one branch per ticket. – Kedar Mar 18 '16 at 15:24
  • 1
    @AndrewWolfe As Kedar mentioned "something" is whatever you want as-per your team's normal branch naming convention. Just one minor correct to Kedar's comment - you can have as many branches per-ticket as you like/need. – charleso Mar 19 '16 at 03:02
  • 1
    @charleso the solution doesn't work , the steps creates a new branch with the desired name and deletes the old one , but this doesn't attach the branch to Jira – Ahmed Korany Apr 10 '16 at 22:51
  • Worked for me. Thanks @charleso – gsteiner Mar 21 '18 at 17:59
  • 2 things guys. It works but 1: you need to wait after creating branch with a good name. Synchronization between Jira and Git takes a while. 2: the names of the issue id are case sensitive. So if the Jira issue is SM-100 the name of the branch cannot be Sm-100-some_nice_branch, but needs to be SM-100-some_nice_branch – goroncy Oct 15 '21 at 10:14
22

Update

As for january 2017 if you have an already exiting branch and you want to attach it to a Jira Issue you can do the following:

  1. Checkout to the branch you want to rename
  2. Execute the following command

    git branch -m JIRA_ISSUE_ID-Whatever

Assuming that mine Jira issue is SO-01 I can do the following:

git branch -m SO-01-Whatever

This will change the name locally, push it to remote with:

git push origin :old_name

Command Syntax

git branch (-m | -M) [<oldbranch>] <newbranch>

Related question for more info

Community
  • 1
  • 1
Sid
  • 14,176
  • 7
  • 40
  • 48
  • 2
    Just an FYI for everyone, it can take a few minutes for the related branch to show up in the JIRA UI, as this was the case for me. – Ay Rue Feb 27 '17 at 21:49
  • Does this change the branch commits in any way? – BamaPookie May 18 '18 at 14:53
  • No. This does not work. The renamed branch never appears under the deverlopment-dropdown in the Jira issue. –  Mar 11 '21 at 08:27
  • It does, you just have to push the branch under new name. The two commands @Sid has written rename the branch locally and delete the remote branch with old name. Now you have to push it with: `git push origin new_name`. – Alexandr Zarubkin Jun 18 '21 at 12:52
19

This is no longer the case. With a common setup between bitbucket and Jira, simply including the issue ID in the commit message will create a link between the commit, and thus the branch, and the issue in Jira.

thegreenpizza
  • 1,440
  • 13
  • 8
  • 2
    pardon me, but what is **exactly** the issue ID? – markroxor Sep 15 '17 at 12:43
  • 1
    it's the issue key. when you edit the issue it appears next to the edit issue, e.g. `Edit issue: PROJ-32`. So just add this name in the commit comment like "fixing stuff on issue #PROJ-32". – Nir Levy Sep 25 '17 at 13:07
  • 1
    This does not seem to link the branch, although the commit will be listed, the branch is not. Not quite the solution, sadly. – Steve Horvath Jan 12 '21 at 03:39
2

I just tested the theory that having the Jira ID in the branch name creates an automatic link.
It does.

To see the effect, you have to push a commit. Then the branch will show up in the Jira. The branch shows up in Jira, but to get an individual commit to show up in Jira I have to refer to the Jira ID in the commit message.

Randy Leberknight
  • 1,363
  • 9
  • 17
1

The web interface option is to branch off a branch but merge back to master in the pull request.

eg:

  1. click create branch in jira
  2. set the repo, branch type and name to what you want
  3. set the branch from to be the existing branch
  4. click create
  5. when creating a pull request set the destination branch to what you want eg master
keza
  • 2,601
  • 2
  • 16
  • 10
0

If you include the JIRA-ID in the branch name, by creating out of an existing commit, all you have to do is:

git push --set-upstream origin <new-branch-name>

and the branch is attached to the JIRA ticket.

  • 1
    If branch is allready pushed (and merged), you can also just create a new branch with *jira-id* embedded, and make a minor change, and then just push and make new PR. – Richard Anderssen Jan 11 '18 at 14:05
-1

Just add a new commit with the Jira issue key in the commit message

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
uwe
  • 1