0

Instead of merging B (Branch) to A(Feature) I merged A(Feature) to B(Branch) in GIT LAB. Here is a graphical representation of what I did. I have this as below. With this the "Branch A2" merges into "feature B2" which is not what i intended. ISSUE: Now if i continue development, the Branch that will have to continue on is "feature B2" instead of "Branch A2"

Branch          A ----A1------ A2 <-- last commit
                 \              \  (merge into feature)
Branch(feature)   B -- -B1- - -- B2 <-- feature now contains Branch "A2" edits.

This is what I wanted. To merged "feature B2" into "Branch A2"; In this way when I continue my development it would be on the "Branch A2" and not "feature B2"

Branch(feature) B ----B1------ B2 <-- last feature commit
                 \              \  (merge into Branch)
Branch            A -- -A1- - -- A2 <-- Branch now contains "B2" feature edits.

What is the most clean way of fixing this so I can get Scenario 2 and continue working on "A2" Branch branch instead of "B2" feature branch? Function wise, they both achieve the same thing whether you merged A2->B2 or B2->A2!
One way comes to mind is that I can rename the Merged branch and call it Branch A2

newb7777
  • 517
  • 5
  • 22

3 Answers3

1

Assuming your changes were not pushed upstream, I think a

git checkout feature
git reset B1 --hard
git checkout branch
git merge feature

should do, where B1 is the commit before the merge.

One way comes to mind is that I can rename the Merged branch and call it Branch A2

When you're "done" with feature anyway, that might even be the cleaner solution in your case.

git branch -d branch branch-tmp # Delete branch
git branch -m feature branch # Rename feature into branch
SVSchmidt
  • 6,269
  • 2
  • 26
  • 37
0

You should be able to revert to the commit before the merge in your branch (see How to revert Git repository to a previous commit?) and then redo the merge in the correct direction.

Don Hosek
  • 981
  • 6
  • 23
0

Just for the benefit of others that may tread on this thread, here is my spiel. Note: All manipulations were done in ** Local Repo although you can do this in ** remote Repo in Git Lab. Once the merge was complete as shown in diagram 1 in original thread:

  1. Create a new branch from the merged branch BUT name it as Branch A or any name that you initially intended it to be. git branch <branch name e.g Branch A>
  2. Checkout the branch git checkout <branch name eg Branch A>. Now your new checked out branch and your Branch B would essentially be the same since there are no code changes since the last merge of branches.
  3. Delete the branch where the merges were from both Local and Remote. git push origin --delete <your remote branch name e.g feature A> and follow it up with git branch -d <your local branch name e.g feature A>
  4. Confirm if this is what you want before pushing it to remote repo by git branch --list and then git branch -a. No worries about history even if you delete these branches since when you push in your commits next time it will show that there was a merge of code and your most recent branch Branch A will have all your changes in it just as you merged branch. It works almost like pointers in C language. The variable name is gone but since you copied the pointer to another name, the address of your change is still intact.
newb7777
  • 517
  • 5
  • 22