aimfeld
aimfeld

Reputation: 3141

Can't merge master to feature branch after reverting a pushed merge

We have been working on a feature branch with git for a while. Every now and then, I merged the master into this feature branch to keep the feature branch in sync with the master and to facilitate merging the feature branch into the master at the end.

Then, I accidentally merged the feature branch into the master (too early, I intended to merge the other way round as before). As the merge had already been pushed, I used git revert -m 1 <merge-commit> to revert the pushed merge according to these instructions.

The revert seems to have worked fine, but now I have the problem that when I try to merge the master into the feature branch, most of the changes in the feature branch get overwritten with the "older" files in the master. It looks like after the revert, the master is considered to have the newer versions of the files. I also suspect that I won't be able to merge the feature branch into the master when we're done.

How can I get back to the initial state where I was able to merge changes in the master into the feature branch without overwriting all other changes made in the feature branch?

Upvotes: 3

Views: 1749

Answers (2)

aimfeld
aimfeld

Reputation: 3141

I performed the following steps and it seems to have fixed the problem:

  1. make backup of the feature branch
  2. merge master into the feature branch (at this point, the "outdated" files from the master are being merged into the feature branch)
  3. delete all files of the feature branch from git and commit
  4. copy backup of the feature branch into the local feature branch
  5. add all files to git, commit, and push

I just tried to make a minor update in the master and merge it into the feature branch. Only the minor update was merged into the feature branch, but not the "outdated" files from the master.

Upvotes: 2

Dave Zych
Dave Zych

Reputation: 21887

You can try performing a checkout of the commit directly before the feature-into-master commit. Branch from that (named master2 or whatever you want). Delete the current master. Then branch from master2 into a new branch named master.

git checkout <hash of commit before feature merge in master>
git checkout -b master2
git branch -D master
git checkout -b master
git branch -D master2

Delete the master branch on github and push your new master up. This might be a bit of a round-about way but it should essentially revert master to where it was before and allow you to continue merging into feature.

Upvotes: 0

Related Questions