llovett
llovett

Reputation: 1469

rebase conflicted, merged commits in git

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.

Upvotes: 1

Views: 83

Answers (2)

user3426575
user3426575

Reputation: 1773

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
    

Upvotes: 1

Wes Hardaker
Wes Hardaker

Reputation: 22252

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

Upvotes: 0

Related Questions