Qwerty
Qwerty

Reputation: 32068

Amend git merge with rebase

tl;dr

I have done a master -> feature merge to update and resolve conflicts before the actual merge.
Can I rebase feature onto master instead and apply the conflict resolution from the merge?

What I have:
o               - TODO merge feature -> master
|\
| o     feature - "Merge remote-tracking branch 'origin/master' into feature"
|/|
o |     master         
| o             - "Implement some new feature here"
| |
...
|/  
o
What I want:
o               - TODO merge feature -> master
|\
| o     feature - "Implement some new feature here"
|/
o       master         
|
...
|
o

Long version

I want to merge a feature branch that is quite old and has conflicts, because other work was merged to master in the mean time. First I updated the feature branch from master by a master -> feature merge. Now I am able to successfully merge the feature branch back without conflicts. But looking at he history now, I realised I should have rebased my feature onto the latest master instead. Unfortunatelly I had to resolve quite an amount of conflicts, can I reuse the current state?

Upvotes: 3

Views: 67

Answers (1)

Philip Pittle
Philip Pittle

Reputation: 12325

As Tim Biegeleisen commented, you can't reuse the merge commit (which is where you resolved your conflicts) in a rebase directly.

The merge conflicts are resolved in a the merge commit, which will drop off when you do the rebase master command.

However, if there merge conflicts were especially painful and/or if you only have a few commits that you're working with, you could follow a workflow of:

  • merge master into feature
  • resolve conflicts
  • do a soft reset of your feature branch
  • stash the changes
  • rebase master
  • replay your stash
  • recreate your commits

By doing the soft reset, the actual file changes after the merge are preserved so you won't need to resolve the conflicts the way you'd need to do if you were just rebasing.

Upvotes: 1

Related Questions