Merge a branch with a clean story

I work on a branch (with others) from the master.

A - B - C - F - G (master) \ D - E (branch-a) 

We periodically merge master into a branch to minimize conflicts later.

 A - B - C - F - G (master) \ \ D - E - H (branch-a) 

In the end, we will want to unite.

 A - B - C - F - G - I - K - M - N (master) \ \ \ / D - E - H - J - L - O (branch-a) 

What is the cleanest way to merge with a master?

  • I want to keep separate commits (i.e. no squash)
  • I will no longer use branch-a , so hash commits can change.
  • I would like to not include merge commits (e.g. H , L ) for merge without conflicts, if possible.

Ideally, it would look like this (if there are no conflicts):

 A - B - C - F - G - I - K - M - N (master) \ / D - E - J - O 

Any ideas on how to do this?

(FYI, this is a question about my workflow. If I had done something even earlier, this is also a legitimate answer.)


UPDATE:

Thinking about it more, I realized that this is often impossible.

For example, if J changed the line that G changed, there would be no way to get this story.

The next best option would be to have this story:

 A - B - C - F - G - I - K - M - D - E - J - O (master) 

Essentially, this is a rebase, but the omission of an unnecessary merger ends.

+6
source share
2 answers

You can do this manually by separating E , and cherry - another, will work in a new branch, and then merge in master . Recovery is also possible, i.e. Do

 git rebase C 

on branch-a . However, this takes a commit from master and places them in your function branch.

 git rebase -i C 

has the same effect, but allows you to skip commits that aren't there, from master and not required on branch-a . Git cannot even know if the commit can interact in any way (for example, changing one file may require a change in another file that was made on master ), so there is no fault-tolerant, fully automatic solution to this problem.

0
source

you can do this when you merge the master into your work branch

  git pull --rebase 

when your commits are made in your working branch, and you want to bring the code from the master to your branch. You can also set this default behavior for specific branches by doing

 git config branch.master.rebase true 

This ensures that your commits are always reapplied, so your story speaks linearly. That is, your commits will be at the top when you ultimately raise the pull request and the story will be clear, while no unnecessary merges will pollute it. Another advantage is that it is easier to choose a cherry tree if you want to keep two or more branches in sync.

0
source

All Articles