I created a branch too early; how can I discard and re-create the branch associated to a Jira ticket
Asked by pierre.rouleau:

We use a Git branch for our work; let's name it **Branch_A**

* At time A, I created a Jira ticket and created the **Branch_B** off ***point A*** of Branch_A
  * Branch_B is associated with the Jira ticket.
* I don't commit anything in Branch_B
* Time passes , several commits are pushed into Branch_A. It's now at ***point P***.
* I would like to have my Branch_B, branched off *point P* instead of the original *point A*.

How do I do that?


Answers:
author: Saxea _Flowie_
body: Hi [@pierre.rouleau](/t5/user/viewprofilepage/user-id/5349754)

You need to do a rebase onto Branch_A:

git checkout Branch_B  
git rebase Branch_A  
git push origin Branch_B --force

comments: author: pierre.rouleau
body: Thanks [@Saxea _Flowie_](/t5/user/viewprofilepage/user-id/5230209) . Will try this.

Always a little nervous to try things like that in a real, super utilized, BitBucket hosted repo environment. I'll read more on rebase and I'll try it in a set of test repos first.

Is there anything I should be watching for that could cause problems?


author: Saxea _Flowie_
body: You can create a backup of your branch before rebasing and also check the log to make sure it looks like how you expect before pushing:

git checkout Branch_B  
**git checkout -b Branch_B_backup**   
git rebase Branch_A  
**git log --graph --oneline --all**   
git push origin Branch_B --force

The critical part is the push force which will rewrite the branch remotely.

One thing that can go wrong is if you have conflicts. Then you need to resolve them and do a \`git rebase --continue\`

Lastly, the commits in this branch will be re-created, so if it's been shared with other team members, you will need to let them know that it has been rebased. So they can adjust their local copies too, by pulling your new commits. There are different ways to handle this, but possibly the simplest is to make sure everyone pushes their changes. You pull their changes, rebase and push. They can then reset their branches: \`git reset --hard origin/Branch_B\`  


author: pierre.rouleau
body: I'm not worried about Branch_B. I'm worried about Branch_A. So I assume that as long as no push is done, all changes are local and as you say the push --force is the critical point. The problem I see is that there's people all over that might be doing something in the Branch_A as I push --force.

I might not even know of their existence... And for sure can't ask them to do anything specific.

The Git repo is hosted on a BitBucket corporate server.

The problem originates from the fact that I created Branch_B sooner than I should have done \*and\* that the Branch_B is tied to a Jira ticket on which I'd push my changes and create a pull request to get the commit approved.

Is there a way to "disconnect" the branch (Branch_B in this case) from the Jira ticket, recreate a new branch, say Branch_C, and "connect" that Branch_C to the Jira ticket?

For the moment my work-around was to create a sub-task of the Jira ticket, create a new branch where I committed my changes, pushed them and create the pull request to Branch_A. But that requires an extra Jira ticket.


author: Saxea _Flowie_
body: I did miss '*I don't commit anything in Branch_B*'. In this case you could just do a \`git pull\` and it would be in sync with Branch_A.

Yeah, all changes are local before pushing.

Re "disconnect", it depends how this is being done in your case. A common way to "connect" or link PRs to Jira tickets is via a reference in the commit message to the ticket. If that is your case a new branch would also work.

Keep in mind that some aspects are particular to your organization, so you might want to ask someone within your org too.


author: pierre.rouleau
body: I used to be quite involved with the way Jira worked where I worked before. But now I sub-contract and asking for the type of info related to the relationship between the Git branch and the Jira ticket did not get me anywhere as I'm not able to find the right person that would know...

And, no. A git pull does not solve my problem. If I pull from Branch_A I'll get the new commits, new branch points from that branch but it won't change the branch-off point where I created the Branch_B. And that's what I want to change.

The real solution would be to delete the Branch_B, recreate it and associate it with the Jita ticket. I was hoping to find a Git only solution but there might not be one.


Tags: bitbucket-server

server