What happens if someone mentions the wrong Jira issue in a Bitbucket Git commit log message?
Asked by eirens:

If someone Git commits to the wrong Jira issue number, how do we fix it with Bitbucket and Jira so that commit/changeset becomes connected to *only* the correct Jira issue?

What's the best practice for this case?


Answers:
author: Theodora Boudale
body: Hi [@eirens](/t5/user/viewprofilepage/user-id/3740914) ,

A commit message in the repo can be changed by rewriting history (so you can replace the commit message with the wrong issue key with a message that has the correct issue key).

This process will create a new commit (with a new hash) that will replace the old commit (the old commit will then become a dangling commit in the repo). You will then need to force push these changes to the repo.

In Jira, the new commit will then appear in the new, correct Jira issue you reference.  
However, the old commit will still show up in the wrong JIRA issue.  
(This is if you use Jira Cloud, I am not sure about the behavior with Jira Server. Are you using Jira Cloud or Server?)

The reason for that is that Jira indexes referenced commits in its database.

When you rewrite history, you are essentially creating a new commit with a new hash (and the old one becomes a dangling commit) and the old commit (with the wrong Jira issue key) has already been indexed in the Jira database.

If you would consider rewriting history in the repo, I can reach out to the Jira team and ask whether it is possible to delete indexed data from the database and re-sync the repo, in case this would help to solve the issue of the old commit still showing in the wrong Jira issue. Please let me know whether you're using Jira Cloud or Jira Server, so I can reach out to the appropriate team.

Please note that it's essential to communicate to your team if you plan to rewrite history. Since the commit's hash will change, other users working on this repo will need to take a fresh clone, to avoid pushing back the old commit. Make sure you also take a full backup of the repo (clone it with the --mirror flag) before you force push any changes.

Feel free to also let me know if you'd like details on how you could rewrite history to change the commit message and also if you have any other questions.

Kind regards,  
Theodora

comments: author: eirens
body: Thank you. I think I'll just make big, red stickers for the team to remind them to never mess this up ever ever ever.


author: eirens
body: [@Theodora Boudale](/t5/user/viewprofilepage/user-id/818086)Thank you for your offer. I would appreciate the instructions you offered. We are using Jira Cloud.


author: Theodora Boudale
body: Hi [@eirens](/t5/user/viewprofilepage/user-id/3740914) ,

Sure, I would just like to ask a few questions first, because I'm seeing different behavior with rewriting history, depending on the branch that the commit belongs to.

Could you please let me know:  
**1.** Are you using more than one branch in this repo?  
**2.** If so, does the commit with the wrong JIRA issue key exist in the main branch, or in a different branch?  
**3.** In case it belongs to a different branch (other than the main one), has this branch been merged in either the main branch, or any other branch?

I'd like to reproduce your exact use case with rewriting, and let you know what the outcome would be before giving you any instructions.

I'll also check with Jira Cloud team whether a resync is possible.

Kind regards,  
Theodora


author: Pratima Maiti
body: Hi [@Theodora Boudale](/t5/user/viewprofilepage/user-id/818086) ,

Is it not possible to prevent the user from committing if the commit message doesn't contain correct JIRA issue ID?


author: eirens
body: [@Theodora Boudale](/t5/user/viewprofilepage/user-id/818086), I think I meant:

Is there a way to block a git commit (on push to Bitbucket) if it mentions a Jira key that doesn't exist? The kind of thing you can do with an SVN precommit hook.


Tags: bitbucket-cloud

cloud

jira