Nirojan Selvanathan
Nirojan Selvanathan

Reputation: 11164

Git Merge and unset reverted commits

I have a develop branch with feature 1, feature 2, and some fixes. For another dev reason we wanted to create another branch from develop and wanted only the fixes in that branch, with out the feature 1 and feature 2. So I create a branch from develop and revert the commit of feature 1 and two. Now we did some development in that new branch. Now I want to merge this branch with the develop branch. But we need the feature 1 and 2 in the develop branch. But if we merge, then the feature 1 and 2 commits will be removed from the develop.

So what is the best way to tackle it in git. One approach is to get the changes of feature 1 and 2 and apply as a patch to the merged develop branch. But the problem is these two features has several changes.

Upvotes: 3

Views: 57

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520878

In the following answer, I assume that when you branched off from develop, the first thing you did was to revert the two feature commits, and after this you did your actual development work. If this not be the case, my answer should still be helpful, though you would have to do some additional steps perhaps to fix your situation.

Perhaps the conceptually easiest solution to your problem would be to just cherry-pick the non revert commits from the new branch into develop:

git checkout develop
git cherry-pick B^..D

Replace B and D with the SHA-1 commit hashes of the commits you want to bring into develop from the new branch. Note that you may get merge conflicts from this, since you are reapplying your work onto a different base.

If you are feeling more adventurous, you could also try using git rebase --onto. Here is a diagram which illustrates the current state of your new branch and develop:

develop   ... ---A---B---C
                  \
newbranch          R1---R2---D---E

And you would like to play the D and E commits from newbranch on top of develop, while ignoring those R1 and R2 revert commits. In other words, you want to end up with this:

develop   ... ---A---B---C---D'---E'
                  \
newbranch          R1---R2---D---E

You can use the following commands to achieve this:

git checkout develop
git rebase --onto C R2 E

The onto command says to take C as a base, and replay all commits whose parent is R2 (but not including R2 itself) up to, and including, commit E. Since C coincides with the HEAD of develop, we are just asking Git to replay commits D and E, in that order on top of the tip of develop.

If you want an excellent explanation of Git's rebase --onto, I recommend reading the answer given by @Enrico here:

I can't understand the behaviour of git rebase --onto

Upvotes: 2

Related Questions