2

I just did a bad thing with my git repo and I don't know how to solve it. I basically have two branches, let's say develop and feature, which I constantly update. At one point I changed a couple of files on the branch develop. I also needed these changes in the feature branch, so instead of doing it again there I made a merge between the two branches only applying these changes (and discarding all the other files changes/conflicts). After that I just made some commits just to the feature branch.

That worked as I wanted but with a big problem. Now I need to merge the two branches (the develop into the feature) completely but when I try to do it nothing happens. I actually know why nothing happens (git thinks the two branches are already merged and the only changes are in the feature branch. It's the same as I try to merge a branch that I just created) but I still need to complete the merge with the old changes. To better understand this in the situation example:

develop  o--c--1-- --2-- --o- 
             \              \
feature       \ --o-- --b-- -\m--o-

b: commit before wrong partial merge
1: changes applied in the m commit (after wrong merge
2: changes NOT applied in the m commit (after wrong merge
m: result of partial merge

As explained in the m commit I just have the changes of the 1 and not the changes of the 2. If I try to merge the develop into the feature now the 2 changes are lost.

I came up with a couple of solutions but I'm not sure if any of that can actually work:

  1. I could checkout to the b commit so I can merge all the changes from the develop. Then I can commit the changes and merge them with the current feature branch
  2. I could create a complete new branch (let's say temp) from the c commit. Then I can copy all the content of the feature branch into the temp branch without actually merging but just copying the files. Then I commit the resulting changes and then merge the new temp branch with the develop. Finally I can copy the resulting content to the feature branch so it's stay aligned.

I think that the second option could actually work well but I'm not really sure.

To finish, in the second option I tried to save the feature branch to keep the repo in order. But I don't really need to preserve the feature branch as I would delete it soon or later when the feature is actually complete.

Anyone knows a better way to help me with this problem?

Thank you in advance

dvdpzzt
  • 73
  • 1
  • 9

1 Answers1

1

Use git cherry-pick.

develop  o--o--1--2--3--4--o- 
             \              \
feature       o--o--o--o--o--o--o-

If first commit is already in your feature branch then:

git checkout feature
git cherry-pick {second-commit-hash}
git cherry-pick {third-commit-hash}
git cherry-pick {fourth-commit-hash}

Then you can compare branches to be sure that your feature branch differs from your develop branch only in changes that you've really introduced in it.