1

I have just finished developing a new feature on its own branch (branch feature) consisting of a dozen or so commits. After running all my tests, I merged this branch into master and resolved many merge conflicts. The process took quite awhile. Only after I finished merging do I realize that I never rebased any of the commits in the feature branch, which is generally my practice.

In short, now I have:

(feature)   B-C-D
(master) A-/     \-E (merge commit in master)

but I want to have:

   F
A-/ \-D

where F = B + C + D "squashed" together.

The way I was going about this was to create another branch master_prime from master starting at commit A. Then, switch to the feature branch and accomplish all the commit squashing I want. Finally, merge feature into master_prime. The only issue is that I'll have to resolve all the merge conflicts again. Is there a way I can replay/pick out the merge commit E so that this isn't necessary?

I have also attempted git rebase --preserve-merges -i <A> as well as git rebase -i <A>, which don't allow me to rebase the commits I want or make me deal with merge conflicts again, respectively.

1
  • Can you elaborate on what about git rebase --preserve-merges -i <A> isn't working for you? I was able to run that exact command and have B, C, D, and E available for rebase operations. Commented Feb 25, 2014 at 22:10

2 Answers 2

1

Use git read-tree to reuse your resolution of the merge conflicts.

I'm assuming you merged feature into master. If you merged master into feature, use master^2 instead of master^ (and vice versa).

  1. Make a backup of your merge commit.

    git branch merge-backup master
    
  2. Squash the commits in feature. Make sure you don't accidentally change anything.

    git checkout feature
    # ... squash the commits ...
    git diff master^2
    
  3. Get rid of the old merge commit.

    git checkout master
    git reset --hard master^
    
  4. Merge the squashed feature branch into master, reusing your old conflict resolution.

    git merge feature
    git read-tree --reset -u merge-backup
    git commit
    
  5. If you are pleased with the results, delete the backup branch.

    git branch -D merge-backup
    
Sign up to request clarification or add additional context in comments.

Comments

0

I think there are probably a number of ways to accomplish what you want, but a couple of things first:

1) you don't need a new master branch if you don't want to. You can use git reset to change the point of the current master branch back to A or something without needing a newly named branch. (you can also create your new master_prime, do what you want to get it to "the best place on earth" and the rename master to master_old and rename master_prime to master.

But that's all besides the point because it's not really what you're asking.

What you want is feature branch feature with all the needed changes from A to D in it. And... the changes required to make the merge. At this point you probably want to rebase, as that will still be a pain. But you can do your squashing and copy your merge edits into place (using reset instead of a new branch left as an exercise to the reader):

git checkout A
git checkout -b feature2
git cherry-pick B..D
... squash away here ...
git cherry-pick E              # copies the merge pain in
git checkout master
git merge feature2

I think

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.