Fadeway
Fadeway

Reputation: 588

How can two backward merges be combined?

I am using a git workflow with a develop branch and multiple feature branches. Sometimes a feature branch needs to be updated to the latest state in develop, using a backward merge. Rarely, two backward merges are done in a row (develop having received new commits between the merges).

Sometimes it makes sense to combine the two merges to simplify the history, ideally without redoing all of the actual merging work. How can this be done? For normal commits I would use git rebase HEAD^^ --interactive and mark the latter commit as fixup, but in the case of merge commits this shows all of the commits brought in by the merges.

Upvotes: 0

Views: 50

Answers (1)

eftshift0
eftshift0

Reputation: 30174

Ok.... what you are doing is technically possible.... say, you just noticed you have 2 backward merges in a row and would like to get them squashed into a single merge.... in a single shot, that can be done, like this (assuming you are working on the feature branch):

git checkout -b test $( git commit-tree -p HEAD~2 -p origin/develop -m "Merging develop" ${HEAD^tree} )

There you created (and checked out) a test branch that has the same working tree as your HEAD revision and has your feature branch - 2 revisions (in other words, the last revision before the 2 backward merges) and the tip of develop. This way, you got what you wanted, you didn't have to redo any merge (and no merge conflicts).

However when working with feature branches, there's no need to do backward merges.... it's much better (if you ask me) to get straight stories for feature branches and for that you should rebase instead of merge. You have 3 revisions.... develop moved? You rebase, you sill get your 3 revisions one after the other.... you write 2 more revisions, develop moves? You rebase again, you still get your 5 revisions one after the other. This simplifies a lot of work in terms of seeing what development was all about and also has advantages if you want to move that development on top of other branches, like a backport, por example.

Upvotes: 1

Related Questions