简体   繁体   中英

git: merge changes from a branch that reverted a previous merge

Sorry for the confusing title, but I couldn't come up with a better one.

Here's my problem. I have 2 branches, develop and feature . I followed this process:

  1. write code in feature
  2. merge feature into develop
  3. discover a bug, revert the merge
  4. write some unrelated code and commit directly to develop .

Now I want to merge the changes I made directly to develop back into feature . The problem is that when I do that, it blows away all the changes I had previously made in feature . I assume this happens because the revert in step 3 is more recent than any code changes in feature , so it applies that revert over the changes.

Is it possible to do what I'm trying to do? The only thing I could think of is to revert the revert in develop , then merge, then revert the revert of the revert. That seems kind of cumbersome.

I think you can rectify the situation in two steps:

1) Do an interactive rebase of develop and remove the revert commit, ie

git rebase -i HEAD~6    # or however far back is the merge commit

Delete the line containing the revert commit and complete the rebase. At this point, develop now has the original merge commit, but no revert.

2) Do a rebase on develop , specifying the hash of the commit from which both develop and feature originated. By default, rebase will ignore the merge commit entirely, thereby removing it:

git checkout feature
git rebase <SHA-1>

where <SHA-1> here is from the commit whence feature branched off from develop .

After these two steps, your develop branch will have neither merge commit nor revert commit, and you should be safe merging it into feature .

I strongly recommend reading the answer given by @torek in this question:

Accidentally merged in wrong branch to mine. Is there a way to remove these unwanted files?

Normally you make a feature branch and you work on that branch to complete your feature. Then you merge your feature to the develop branch and after some time the develop state goes live.

In your case if you have changes on the develop branch i would prefer a rebase on your feature branch. Then you get all changes and your feature branch is rewritten to the last commit of your dev branch.

Atlassian has a good tutorial how you can work with feature branches.

https://www.atlassian.com/git/tutorials/comparing-workflows#feature-branch-workflow

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM