Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge master containing reverted commits to branch

I have master branch with initial state:

touch file
git add file
git commit -m "initial"

Then I did some changes:

echo "123" >> file
git commit -am "feature1"

which really belong to a feature branch, so I branched:

git branch TEST

and reverted change in master (I don't know if this is the best way to do it but it did what needed at the time):

git revert <last commit hash>

Then I did some more changes on feature branch:

git checkout TEST
echo "456" >> file
git commit -am "feature 1 continued"

And now I have problem merging master to it. What I want is to be able to merge all future changes in master to a TEST branch and keep my feature branch changes.

Code is already pushed to a remote.

like image 418
NSPKUWCExi2pr8wVoGNk Avatar asked Mar 21 '23 08:03

NSPKUWCExi2pr8wVoGNk


1 Answers

Revert the revert

To be able to merge the TEST branch you'll need to revert the revert, so that the TEST history can be applied cleanly.

I.e. whereas originally the master branch history was:

$ git checkout master
$ git log --oneline
yyyyyyy Revert "wanted change"
xxxxxxx wanted change
....

And the TEST branch history is:

$ git checkout TEST
$ git log --oneline
aaaaaaa Test development commits
xxxxxxx wanted change
....

What you'll need is to achieve this:

$ git checkout TEST
$ git log --oneline
aaaaaaa Test development commits
zzzzzzz Revert "Revert "wanted change"" # <-
yyyyyyy Revert "wanted change"
xxxxxxx wanted change
....

I.e. a checkout at xxxxxxx and at zzzzzzz would be exactly the same.

Use an intermediary branch

To do that, a relatively easy way is to use an intermediary branch:

$ git checkout master
$ git checkout -b merge-resolution
$ git revert yyyyyyy
$ git merge TEST

And then merge that branch with master:

$ git checkout master
$ git merge merge-resolution
$ git branch -d merge-resolution

This should result in a clean/consistent history in master, with the changes of TEST cleanly applied.

like image 92
AD7six Avatar answered Apr 07 '23 19:04

AD7six