Link already committed code in bitbucket repo to jira epics or user stories
Asked by Ashlee:

We have committed code in bitbucket repo to one of our environments without any branching related to issues in jira. Is it possible to create branches or link those issues or user stories in jira matching with the already committed code for traceability purposes. Thanks


Answers:
author: Theodora Boudale
body: Hi [@Ashlee](/t5/user/viewprofilepage/user-id/4730513) and welcome to the community!

**++Regarding creating branches:++**

This depends on how your commit graph looks now and what you want to achieve.

Let's say there 3 commits that you want to link to Jira issues:

* are they the latest 3 commits in the branch where you created them? Or are there more commits after them?
* Are any of these 3 commits a merge commit?
* And do you want to remove them completely from the branch they are now into a new branch, which you can later merge to the branch they are now?

<br />

**++Regarding linking Jira issues to existing commits:++**

It is possible to change the commit messages of the existing commits so that you add the corresponding Jira issue key in these messages (without creating or changing any branches).

Please note though that this process includes ++rewriting history++ , which means that the ++hashes of these commits (and commits after them) will change++.

* It would be good to communicate this to your team if you decide to do it, so everybody takes a fresh clone of the repo afterwards to avoid pushing the commits with the old hashes.
* Please take a backup of the repo (a clone with the --mirror flag) in case anything goes wrong and you want to restore the repo to the previous state.

<br />

I would also recommend creating a fork of the repo to test the solution I am suggesting below and pushing the changes to the fork first, to inspect if it's in a good state before you push to the existing repo.

<br />

The process to change commits messages is the following:

**1.** If you want to change the commit messages e.g. of the 9th and 10th latest commits, you can run

```
git rebase -i HEAD~10
```

This command will display the latest 10 commits in your default text editor.

**2.** Move to the lines of the commits whose message you want to change, and replace the word **pick** with the word **reword**. Leave the other lines as they are, save the changes and close the editor.

**3.** Then, for each commit that you chose to **reword** in the previous step, a new text editor window will open.  
In there, you can change the commit message of that specific commit and add the Jira issue key, save the changes and close the editor.

**4.** After you finish this process, you can run a **git log** to confirm that the commit messages have been changed to include the Jira issue keys.

**5.** You will need to push these changes with

**git push --force \<remoteName\> \<branchName\>**

since you have rewritten history.

Again, test this by pushing to a fork of the repo first, to inspect if the repo is in a good state after the push.

Make sure that no one has pushed any newer commits to that branch that you don't have locally, as they will get removed if you force push.

If you have any questions, please feel free to let me know.

Kind regards,  
Theodora


Tags: bitbucket-cloud

cloud

jira